我正在使用 Postman 和 Advanced REST 客户端为以下代码创建基本 POST 请求 -
'use strict';
var express = require('express');
var bodyParser = require('body-parser');
var app = express();
var http = require('http');
// configure the app to use bodyParser()
app.use(bodyParser.urlencoded({
extended: false
}));
app.use(bodyParser.json());
//app.listen(6666);
http.createServer(function (req, res) {
h2s(req, res);
}).listen(6666, '127.0.0.1');
console.log('Server running at http://127.0.0.1:6666/');
module.exports = function h2s(req, res) {
console.log("inside h2s");
app.use(function (req, res) {
console.log("req.body : " + req.body);
res.send("OK");
});
}
但是,当我调试时,我发现“req 对象树”中缺少 req.body。更奇怪的是我对 req.headers 所做的所有更改都在 req 对象树中可用。
看起来我似乎犯了一个微不足道的错误,但我无法弄清楚。解决了一个小时左右的故障,但没有运气!
你们中的任何人都可以弄清楚为什么 req.body 似乎从 req 对象树中丢失了吗?
会对我有很大的帮助。谢谢!