繁体   English   中英

node.js express req.body undefined

[英]node.js express req.body undefined

var express = require('express'); 
var app = express(); 
var PORT = 3000; 

const bodyParser = require('body-parser')
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({extended: true}));
 
app.post('/', function (req, res) { 
  console.log(req.body); 
  console.log(req.body.username);
  res.end(); 
}); 

app.listen(PORT, function(err){ 
  if (err) console.log(err); 
  console.log("Server listening on PORT", PORT); 
});

通过 postman 测试: Post: http://localhost:3000 { "username":"userabc", "password":"passwd1234" }

但是,结果是:{} undefined

尝试在请求标头中设置 Content-Type: application/json

您需要在请求中将 Content-Type 设置为 application/json。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM