EN
Express.js - pass path parameter
3 points
In this article, we would like to show you how to get the path parameter in Express.js.
Quick solution:
xxxxxxxxxx
1
app.get('/path/to/resource/:parameterName', (request, response) => {
2
const parameterName = request.params.parameterName;
3
// ...
4
});
In this section, we present a practical example with GET
method.
xxxxxxxxxx
1
const express = require('express'); // npm install express
2
3
const app = express();
4
5
app.get('/users/:id', (request, response) => {
6
const userId = request.params.id;
7
// Do some operations here ...
8
response.send(user);
9
});
10
11
app.listen(3000);
Where: when GET
request to /users/5
path is performed then request.params.id
is set to 5
.