我正在尝试使用node-http-proxy作为反向代理,但我似乎无法使POST和PUT请求起作用.文件server1.js是反向代理(至少对于具有url"/ forward-this"的请求),server2.js是接收代理请求的服务器.请解释一下我做错了什么.
这是server1.js的代码:
// File: server1.js
//
var http = require('http');
var httpProxy = require('http-proxy');
httpProxy.createServer(function (req, res, proxy) {
if (req.method == 'POST' || req.method == 'PUT') {
req.body = '';
req.addListener('data', function(chunk) {
req.body += chunk;
});
req.addListener('end', function() {
processRequest(req, res, proxy);
});
} else {
processRequest(req, res, proxy);
}
}).listen(8080);
function processRequest(req, res, proxy) {
if (req.url == '/forward-this') {
console.log(req.method + ": " + req.url + "=> I'm going to forward this.");
proxy.proxyRequest(req, res, { …Run Code Online (Sandbox Code Playgroud)