Route parameters
- Node.js course
- Intermediate
- Lesson 36/116
- 5 min
Learn URL parameters.
Route parameters
In Express you capture a variable part of a URL with a route parameter. The parameter name starts with a colon: /users/:id. When the request is /users/42, req.params.id is "42".
Route parameters always come as strings; if you need a number you convert it with Number(req.params.id). You can have multiple parameters: /posts/:postId/comments/:commentId gives req.params.postId and req.params.commentId. This is the basic way to handle dynamic URLs with a single route. For example, a user profile, a blog post or a product page is served by one route.
Accessing with req.params
Route parameters are accessed through the req.params object. In the /users/:id route, req.params.id gives the parameter value. Example: app.get("/users/:id", (req, res) => { res.send("User: " + req.params.id); });.
This produces the response "User: 42" for a /users/42 request. The parameter name is what follows the colon in the route; if it is :id the name is id. You can define multiple parameters and each is found in req.params under the same name. This is the basis of REST APIs where each resource is identified by a URL: /users/:id represents one user, /products/:id one product.
Practice Write a user parameter route
Define the route app.get("/users/:id", (req, res) => { res.send("User: " + req.params.id); }). The server requests /users/42.
Starter code
const express = require("express");
const app = express();
// Define a /users/:id route
// Read the parameter and respond
// write here
const server = app.listen(3000, () => {
const http = require("http");
http.get("http://localhost:3000/users/42", (res) => {
let body = "";
res.on("data", (c) => body += c);
res.on("end", () => { console.log("Reply: " + body); server.close(); });
});
});Sample solution
const express = require("express");
const app = express();
app.get("/users/:id", (req, res) => {
res.send("User: " + req.params.id);
});
const server = app.listen(3000, () => {
const http = require("http");
http.get("http://localhost:3000/users/42", (res) => {
let body = "";
res.on("data", (c) => body += c);
res.on("end", () => { console.log("Reply: " + body); server.close(); });
});
});Expected output
Yanıt: Kullanıcı: 42
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 What does /users/7 give on the /users/:id route?
app.get("/users/:id", (req, res) => res.send(req.params.id))- req.params = {}
- req.params.id = undefined
- req.params.id = 7
- req.params.id = "7" Correct answer
Explanation: The parameter always comes as a string: "7".
2 Complete the params access
app.get("/users/:id", (req, res) => {
res.send("Kullanıcı: " + req.____.id);
});
app.get("/users/:id", (req, res) => {
res.send("Kullanıcı: " + req.params.id);
});
Explanation: req.params.id reads the parameter.
3 Match each piece to its role
| Term | Meaning |
|---|---|
:id |
Defines a route parameter |
req.params |
Parameter object |
/users/42 |
Actual request path |
Number(...) |
Converts string to number |