Route parameters let you capture parts of a URL as variables. This helps you handle dynamic content like user profiles or product pages.
Route parameters in Express
app.get('/path/:parameterName', (req, res) => { const param = req.params.parameterName; res.send(`Value is ${param}`); });
Route parameters start with a colon : in the route path.
You access the parameter value inside the handler with req.params.parameterName.
id from the URL like /user/123.app.get('/user/:id', (req, res) => { res.send(`User ID is ${req.params.id}`); });
category and id.app.get('/product/:category/:id', (req, res) => { res.send(`Category: ${req.params.category}, Product ID: ${req.params.id}`); });
app.get('/post/:postId/comment/:commentId', (req, res) => { res.send(`Post: ${req.params.postId}, Comment: ${req.params.commentId}`); });
This simple Express app listens on port 3000. It has one route that takes a userId from the URL and sends it back in the response.
Try visiting http://localhost:3000/user/42 in your browser to see the output.
import express from 'express'; const app = express(); const port = 3000; app.get('/user/:userId', (req, res) => { const userId = req.params.userId; res.send(`User ID received: ${userId}`); }); app.listen(port, () => { console.log(`Server running on http://localhost:${port}`); });
Route parameters only capture the part of the URL where you put the : placeholder.
If you want to accept optional parameters, you can add a question mark like :param?.
Always validate or sanitize route parameters before using them to avoid security issues.
Route parameters let you get dynamic values from the URL.
Use req.params to access these values inside your route handler.
This helps build flexible routes for things like user profiles, products, or posts.