【发布时间】:2017-12-11 16:01:36
【问题描述】:
我有一个管理登录表单页面的 Express 服务器:
const app = express();
// section A
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.urlencoded());
app.get('/login', userController.getLogin);
app.post('/login', userController.postLogin);
它运行正确。
然后我有另一个读取 RAW DATA 的控制器:
// section B
const concat = require('concat-stream');
app.use(function(req, res, next) {
req.pipe(concat(function(data: any) {
req.body = data;
next();
}));
});
app.post('*', otherController.post);
export let post = (req: any, res: Response) => {
console.log(req.body); //i see RAW DATA
res.end('n');
}
它也很好用。
但如果我加入这两个部分,第二部分将停止工作。
我怎么说只能在 B 部分使用req.pipe?
【问题讨论】:
-
可以设置req.bodyRaw = data;然后使用它
-
我认为这不是正确的解决方案,请参阅coderwall.com/p/qrjfcw/capture-raw-post-body-in-express-js 你有例子吗?
标签: javascript node.js express http-post raw-data