user4798115 user4798115 - 13 days ago 12
Node.js Question

express deprecated req.param(name): Use req.params, req.body, or req.query instead app.js:34:22

I am newbie to node.js and have developed the following application. In this application I am expecting value 'isAdmin' : true in reponse, but I dont get it. Instead I see the following error Why ?

express deprecated req.param(name): Use req.params, req.body, or req.query instead app.js:34:22
express deprecated req.param(name): Use req.params, req.body, or req.query instead app.js:35:26


Something certainly is missing out. Please guide.

var express = require('express');
var bodyParser = require('body-parser');


var app = express();
//app.use(express.urlencoded());
app.use(bodyParser.urlencoded({
extended: true
}));


function authUser(request, response, next){
var user = {
name : 'Raj',
admin : true
};

request.user = user;
next();
};

app.use(authUser);

app.get('/', function(request, response){
response.send({
foo : 'bar',
person : 'Raj',
isAdmin : request.params.user.admin
});
});


app.post('/doStuff', function(request, response){
var param = request.param('foo');
var firstName = request.param('firstName');
response.send({
foo : param,
firstName : firstName
});
});

app.listen(3000);


enter image description here

Answer

This is because req.params('firstName') is deprecated as of Express v4.11.0

You should be using standard object dot notation req.params.firstName instead.

app.use((req, res, next) => {
    req.user = {
        name : 'Raj',
        admin : true
    };

    return next();
});

app.get('/',(req, res) => {
    return res.send({
        foo : 'bar',
        person : 'Raj',
        isAdmin : req.user.admin
    });
});

app.post('/doStuff', (req, res) => {
    return res.send({
        foo : req.params.foo,
        firstName : req.params.firstName,
        isAdmin: req.user.admin
    });
});