我需要異步修改請求主體。東西沿線:node http-proxy:異步修改請求主體
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);
});
}
});
當我運行這個我得到的錯誤說不能modfiy頭一旦他們已經設置。這是有道理的。
在我準備好之前如何停止發送請求?我已經着眼於從proxyReq中刪除各種聽衆而沒有成功。
試試這個https://github.com/nodejitsu/node-http-proxy#setup-a-stand-alone-proxy-server-with-latency –