我想創建一個簡單的express express中間件過濾器,它應該查看POST正文並確定它是否應該通過管道連接到正確的服務器或阻止。如果我使用body-parser
,看起來確實可以得到req.body
,但是當我將請求傳送到另一個外部服務器時,它會從請求中消失。在請求之前阻止快速的身體分析器去除身體.pipe
我試圖解決這一問題的方法是:
//Router
router.route('')
.post(authController.isAuthenticated,
urlHelper.bodyParser, //Without these two, everything pipes correctly
urlHelper.filterBasedOnSomething, //Without these two, everything pipes correctly
urlHelper.pipeToTrustedZone);
在urlHelper:
const filterBasedOnSomething = function (req, res, next) {
if (req.body
&& req.body.something === 'some string'
&& req.body.somethingElse === 'some other value') {
return res.status(403).send("You shall not pass");
} else {
next();
}
}
const pipeToTrustedZone = function (req, res) {
//Here we are figuring out the correct forwarding url. This is simplified a little for the question, but it is well tested and works.
var url = getProxiedPath(req.baseUrl) + req.originalUrl;
req.pipe(request({ qs: req.query, uri: url }).on('error', function (err) {
log.info(err);
return res.sendStatus(400);
}))
.pipe(res);
};
module.exports = {
bodyParser: require('body-parser').json(),
filterBasedOnSomething: filterBasedOnSomething,
pipeToTrustedZone: pipeToTrustedZone,
//..
}
這似乎給了我在我的過濾方法req.body
,但身體消耗並且在管道向前未收到。我嘗試了多種東西,例如req.emit('data', JSON.stringify(req.body));
和req.write(..
,但它似乎都失敗了。
有沒有辦法讓我在查看請求主體之前進一步管道而不刪除它?或者我的方法存在繼承問題?
我發現多個GitHub的問題等等有關這個問題,但我還沒有成功地獲得任何這些方法的工作:
https://github.com/expressjs/body-parser/issues/74
Writing express middleware to get raw request body before body-parser
https://github.com/expressjs/body-parser/issues/94
我明白了。你知道我該如何記錄數據並在以後重新發送?我嘗試了'req.emit('data',bodyData);'有和沒有'req.emit('end');'。我試了一大堆東西。也許有一種方法可以重新創建'req'變量並進一步移植? – Automatico
@ Cort3z如果你不需要管道完全相同的流,但只是相同的數據,那麼你可能能夠將你的身體添加到請求。查看更新後的答案。 – rsp
我收到'{「message」:「write after end」,「name」:「Error」,「stack」:「錯誤:在ClientRequest.write(_http_outgoing.js:485:15) n'設置'body:req.body'時是否需要成爲字符串化的json? – Automatico