Route Parameters in Express JS

RMAG news

Route parameters are placeholders in the URL pattern of an express route. It allows you to capture dynamic values from the URL, such as user IDs, product names, or any other variable data. Route params are defined in the route path by prefixing a colon (“:”) followed by the parameter name.

app.get(“/users/:id”, (req, res) => {
const userId = req.params.id
res.send(`User id: ${userId}`)
})

Inside the route handler function, you can access route parameters using req.params. It is an object containing key value pairs, where the keys are the parameter names defined in the route path.

Leave a Reply

Your email address will not be published. Required fields are marked *