在Express节点中检索JSON POST参数

时间:2019-05-08 13:03:21

标签: node.js express postman postman-pre-request-script

这是我第一次使用节点js。我正在通过邮递员发送发帖请求。问题是我无法获得这些参数。它说未定义或为空

enter image description here

这是我到目前为止尝试过的

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版本为4.16.4

这就是控制台上显示的内容

enter image description here enter image description here

1 个答案:

答案 0 :(得分:0)

设置标题:{"Content-Type": "application/json"}。更改它可以解决问题。