我有以下代码:
var express = require('express');
var app = express.createServer();
var request = require('request');
app.use(myMiddleware);
app.listen(5010);
var payload = { id: 1 };
request({
method: 'POST',
body:JSON.stringify(payload),
url: 'http://localhost:5000'
}, function(err, res, body) {
console.info("Request Done");
})
在我的中间件代码中,我想解析正文并提取请求 ID,但由于某种原因,以下代码不起作用(有效负载未定义):
var myMiddleware= function (req, res, next){
var payload = req.body;
if (payload.id === 1) console.info("first request!!!!!");
next();
}
当我尝试打印“有效负载”时,我得到的只是[object Object]
.
您能告诉我如何提取 id,以及如何打印有效负载对象的属性吗?
谢谢,李