Languages
[Edit]
EN

Node.js / Express.js - get path variable

0 points
Created by:
Dirask JS Member
3460

In this article, we would like to show you how to get the path parameter in Express.js. 

We can get to the parameters we get in the path (for example ID) using the params property of the received request.

Note: 

In place of the path, where we expect the parameter, we put :customVariable and we refer to it by request.params.customVariable.

Example with DELETE method: 

const express = require('express');
const app = express();

app.delete('/user/:id', (request, response) => {
    const id = request.params.id;
    // ... delete user with given id

    response.send('User removed');
});

app.listen(3000);

When received DELETE request is /user/5, then request.params.id will be 5. 

Native Advertising
­čÜÇ
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

ÔŁĄ´ŞĆ­čĺ╗ ­čÖé

Join