Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Route Parameters | Section
Building APIs with Express.js

bookRoute Parameters

Swipe to show menu

Sometimes you need dynamic routes that change based on input, such as an ID.

Express allows you to define route parameters using :.

app.get('/users/:id', (req, res) => {
  const userId = req.params.id;

  res.send(`User ID is ${userId}`);
});

Here, :id is a dynamic value. When a request comes in, Express extracts it and makes it available in req.params.

For example:

  • '/users/1': id = 1;
  • '/users/42': id = 42.

This is commonly used to get specific data, such as a user by ID or a product by ID.

question mark

Where are route parameters stored?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 1. Chapter 7

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

Section 1. Chapter 7
some-alt