我正在使用node-http-proxy
尝试在我的节点/快速Web服务器中代理请求。我正在尝试在路由级将正文附加到特定请求,但是收到的响应表明req.body
不存在。
const proxy = httpProxy.createProxyServer({
changeOrigin: true,
target: process.env.API_URL,
port: 80
});
server.post('/api/login_check', (req, res) => {
req.url = req.url.replace('/api', '');
req.headers['Content-Type'] = 'application/json';
req.headers['accept'] = 'application/json';
req.body = JSON.stringify({
username: process.env.USERNAME,
password: process.env.PASSWORD
});
proxy.web(req, res);
});
是否可以在node-http-proxy
的路由级别上做到这一点?
答案 0 :(得分:0)
如果我正确理解您的意思。
proxy.on('proxyReq', function(proxyReq, req, res, options) {
//her you can create condition identifying your path
if(req.body && req.url == "/user" && req.method=="POST"){
let bodyData = JSON.stringify(req.body);
proxyReq.setHeader('Content-Length', Buffer.byteLength(bodyData));
// Stream the content
proxyReq.write(bodyData);
}
});