đ Introduction
Route parameters are dynamic values embedded directly within a URL path. They allow an Express.js application to capture specific information from the URL, such as user IDs, product IDs, or category names. Route parameters are defined using a colon (:) and are accessed through the req.params object.
Information
đ Prerequisites
- Node.js installed.
- Express.js installed.
- A basic Express.js server created.
- Basic understanding of Express routing.
â What Are Route Parameters?
Route parameters are placeholders within a route path that capture values from the requested URL. Each parameter is prefixed with a colon (:), and Express automatically stores the extracted values inside the req.params object.
| Route | Request URL | Parameter Value |
|---|---|---|
| /users/:id | /users/101 | id = 101 |
| /products/:productId | /products/25 | productId = 25 |
| /posts/:slug | /posts/express-routing | slug = express-routing |
đ Route Parameter Structure
đ Creating a Route with Parameters
Single Route Parameter
const express = require('express');
const app = express();
app.get('/users/:id', (req, res) => {
const userId = req.params.id;
res.send(`User ID: ${userId}`);
});
app.listen(3000);If a client visits http://localhost:3000/users/101, Express extracts the value 101 and stores it in req.params.id.
đ¤ Example Response
Response
User ID: 101đ Multiple Route Parameters
A route can contain more than one parameter. Each parameter is identified by its unique name.
Multiple Route Parameters
app.get('/users/:userId/orders/:orderId', (req, res) => {
res.json({
userId: req.params.userId,
orderId: req.params.orderId
});
});Example request:
/users/15/orders/250
đ¤ JSON Response
Response
{
"userId": "15",
"orderId": "250"
}đĸ Converting Route Parameters
Route parameter values are received as strings. Convert them when numeric operations are required.
Convert Parameter to Number
app.get('/products/:id', (req, res) => {
const productId = Number(req.params.id);
res.send(`Product ID: ${productId}`);
});Tip
đ Route Parameters with Multiple Segments
Category and Product
app.get('/category/:category/product/:productId', (req, res) => {
res.json({
category: req.params.category,
productId: req.params.productId
});
});Example request:
/category/electronics/product/101
đ Common Use Cases
| Use Case | Example Route |
|---|---|
| User Profile | /users/:id |
| Product Details | /products/:id |
| Blog Post | /posts/:slug |
| Order Information | /orders/:orderId |
| Category Products | /categories/:name |
đ Route Matching Process
đ Route Parameters vs Query Parameters
| Feature | Route Parameters | Query Parameters |
|---|---|---|
| Location | Part of the URL path | After the ? symbol |
| Access Method | req.params | req.query |
| Purpose | Identify a specific resource | Filter, search, sort, or paginate data |
| Example | /users/101 | /users?page=2 |
â ī¸ Validating Route Parameters
Simple Validation
app.get('/users/:id', (req, res) => {
const id = Number(req.params.id);
if (isNaN(id)) {
return res.status(400).send('Invalid User ID');
}
res.send(`User ID: ${id}`);
});Warning
đĄ Best Practices
- Use route parameters to identify specific resources.
- Choose descriptive parameter names such as :userId or :productId.
- Validate parameter values before processing them.
- Convert numeric parameters to numbers when required.
- Use query parameters for optional filtering and route parameters for resource identification.
- Keep route URLs clean, readable, and RESTful.
Best Practice
đ Learn More
Explore the official Express.js documentation for routing:
âĸ Express.js Routing Guide
âĸ Express.js Request - req.params