Files
freeCodeCamp/curriculum/challenges/english/blocks/lecture-understanding-routing-in-express-js/69a91831ef910efdde054a5a.md
T
2026-04-24 18:25:01 +00:00

3.8 KiB
Raw Blame History

id, title, challengeType, dashedName
id title challengeType dashedName
69a91831ef910efdde054a5a What Are Route Parameters? 19 what-are-route-parameters

--description--

In Express, route parameters are dynamic values embedded in the URL path. They let your application capture parts of the URL that can change with each request. This is useful for building dynamic pages, like displaying a specific user, blog post, or product based on the URL.

Heres an example:

// Route with a parameter in the path
app.get("/post/:postId", (req, res) => {
  const postId = req.params.postId;
  res.send(`Viewing post with ID: ${postId}`);
});

Here, the route listens for GET requests to /post/:postId.

  • :postId is a route parameter, a placeholder in the URL that will be replaced by an actual value. For example, a request to /post/42 matches this route, and 42 becomes the value of postId.

  • You can access route parameters using req.params. The req.params object contains all route parameters, and in this case, req.params.postId contains the value from the URL, 42.

Finally, res.send() sends a response back to the client. So if the URL is /post/42, the client sees Viewing post with ID: 42.

So as you can see, route parameters make your routes flexible and dynamic without needing a separate route for every possible value.

Now that you understand how route parameters work, lets go back to an example from the last lesson and see how you can use dynamic routes for different types of menus:

app.get("/menu/:category", (req, res) => {
  const category = req.params.category;
  res.send(`You are viewing the ${category} menu!`);
});

And heres a breakdown of how this works:

  1. A user visits http://localhost:3000/menu/drinks.

  2. The server matches this request to the route /menu/:category and captures the category parameter as drinks.

  3. The server sends this response back to the browser: You are viewing the drinks menu!

Similarly, if the user visits http://localhost:3000/menu/food, the server responds with the following: You are viewing the food menu!

Route parameters let you display dynamic content based on the URL. Instead of creating a separate route for every category, you can use a single route to handle multiple categories like drinks, food, desserts, and more, simply by changing the URL.

--questions--

--text--

What is the role of :postId in the route /post/:postId?

--answers--

It is a static part of the URL.

--feedback--

: indicates a parameter, not a fixed segment.


It captures a dynamic value from the URL.


It is a query parameter.

--feedback--

Query parameters appear after ?, not within the path.


It defines a custom header.

--feedback--

Route parameters live in the URL path, not headers.

--video-solution--

2

--text--

What would req.params.postId contain when the URL is /post/100?

--answers--

100


postId

--feedback--

req.params holds the actual values captured from the URL.


/post/100

--feedback--

That is the full path; params only expose the dynamic segment.


An empty string

--feedback--

The placeholder must be filled by the actual value in the URL.

--video-solution--

1

--text--

How can you use route parameters in your application?

--answers--

To store static values in the URL.

--feedback--

Static values dont benefit from parameter placeholders.


To dynamically capture parts of the URL for processing.


To validate user input.

--feedback--

Validation may occur after capturing the value, but the parameter itself captures data.


To define headers for requests.

--feedback--

Route parameters belong to the path and do not define headers.

--video-solution--

2