0

this is the first time I am using node js. I am sending post request through Postman. The problem is I am unable to get these params. It says undefined or empty

enter image description here

This is what I have tried so far

var express = require("express");
var app = express();


var bodyParser = require('body-parser')
app.use( bodyParser.json() );       // to support JSON-encoded bodies
app.use(bodyParser.urlencoded({     // to support URL-encoded bodies
    extended: true
}));


// support encoded bodies
app.listen(3000, () => {
    console.log("Server running on port 3000");
});






app.post('/login',function(req,res){

    console.log('request =' + JSON.stringify(req.body))
    console.log(req.body);

    var user_name=req.body.user;
    var password=req.body.password;
    res.send(req.body);

    console.log("User name = "+user_name+", password is "+password);
    res.end("yes");
});

express version is 4.16.4

This is what appears on the console

enter image description here enter image description here

user1hjgjhgjhggjhg
  • 1,237
  • 4
  • 15
  • 34

1 Answers1

1

set headers: {"Content-Type": "application/json"} in the request. Changing it will solve the problem.