【发布时间】:2017-05-15 11:27:15
【问题描述】:
我需要异步修改请求正文。类似这样的东西:
proxy.on('proxyReq', function(proxyReq, req, res, options) {
if(req.body) {
new Promise(function(resolve){
setTimeout(function() { // wait for the db to return
'use strict';
req.body.text += 'test';
let bodyData = JSON.stringify(req.body);
proxyReq.setHeader('Content-Type','application/json');
proxyReq.setHeader('Content-Length', Buffer.byteLength(bodyData));
// stream the content
proxyReq.write(bodyData);
resolve();
},1);
});
}
});
当我运行它时,我收到错误消息,说一旦设置了标头就无法修改标头。这是有道理的。
在我准备好之前如何停止发送请求?我已经看过从 proxyReq 中删除各种侦听器但没有成功..
【问题讨论】: