Express +邮差,请求是空的

我知道这已被多次询问,但我一直在四处寻找,仍然无法find我的问题的答案。

这是我的代码,我确定在定义路由之前使用和configurationbody parser。 我只与bodyParser使用.json(),因为现在我只testing一个POST函数,但我甚至尝试过使用app.use(bodyParser.urlencoded({extended:true}));

var express = require('express'), bodyParser = require('body-parser'), app = express(); app.use(bodyParser.json()); app.set('port', (process.env.PORT || 5000)); app.listen(app.get('port'), function() { console.log("Node app is running at localhost:" + app.get('port')) }); app.post('/itemSearch', function(req, res) { //var Keywords = req.body.Keywords; console.log("Yoooooo"); console.log(req.headers); console.log(req.body); res.status(200).send("yay"); }); 

以下是我如何使用邮差testing这条路线。 在这里输入图像描述

这里是我收到的回应

 Node app is running at localhost:5000 Yoooooo { host: 'localhost:5000', connection: 'keep-alive', 'content-length': '146', 'cache-control': 'no-cache', origin: 'chrome-extension://fhbjgbiflinjbdggehcddcbncdddomop', 'content-type': 'multipart/form-data; boundary=----WebKitFormBoundarynJtRFnukjOQDaHgU', 'user-agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36', 'postman-token': '984b101b-7780-5d6e-5a24-ad2c89b492fc', accept: '*/*', 'accept-encoding': 'gzip, deflate', 'accept-language': 'en-GB,en-US;q=0.8,en;q=0.6' } {} 

在这一点上,我真的很感激任何帮助。 谢谢。

AFAIK你需要使用身体parsing器: https : //github.com/expressjs/body-parser

 bodyParser = require('body-parser').json(); app.post('/itemSearch', bodyParser, function(req, res) { //var Keywords = req.body.Keywords; console.log("Yoooooo"); console.log(req.headers); console.log(req.body); res.status(200).send("yay"); }); 

然后用PostMan将身体设置为raw json:

 { "test": "yay" } 

花了几个小时后,我意识到需要将邮递员中的Rawtypes更改为JSON 在这里输入图像描述