Sometimes, we want to get a URL parameter in Express and Node.js.
In this article, we’ll look at how to get a URL parameter in Express and Node.js.
How to get a URL parameter in Express and Node.js?
To get a URL parameter in Express and Node.js, we can use the req.params
property.
For instance, we write
app.get('/p/:tagId', (req, res) => {
res.send(req.params.tagId);
});
to get the tagId
URL parameter with req.params.tagId
in the GET route’s route handler.
Then if we make a GET reqest to /p/1, we see that req.params.tagId
is 1.
Conclusion
To get a URL parameter in Express and Node.js, we can use the req.params
property.