मैं नोड-http-प्रॉक्सी को रिवर्स प्रॉक्सी के रूप में उपयोग करने का प्रयास कर रहा हूं, लेकिन मुझे काम करने के लिए POST और PUT अनुरोध नहीं मिल रहे हैं। फ़ाइल server1.js रिवर्स प्रॉक्सी है (कम से कम url "/ forward-this" के अनुरोध के लिए) और server2.js वह सर्वर है जो प्रॉक्सी अनुरोध प्राप्त करता है। कृपया बताएं कि मैं गलत तरीके से क्या कर रहा हूं।प्रॉक्सी क्लाइंट पोस्ट और PUT अनुरोधों को रिवर्स कैसे करें node-http-proxy
यहाँ 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, {
host: 'localhost',
port: 8855
});
} else {
console.log(req.method + ": " + req.url + "=> I'm handling this.");
res.writeHead(200, { "Content-Type": "text/plain" });
res.write("Server #1 responding to " + req.method + ": " + req.url + "\n");
res.end();
}
}
और यहाँ server2.js के लिए कोड है:
// File: server2.js
//
var http = require('http');
http.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);
});
} else {
processRequest(req, res);
}
}).listen(8855);
function processRequest(req, res) {
console.log(req.method + ": " + req.url + "=> I'm handling this.");
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.write("Server #2 responding to " + req.method + ': url=' + req.url + '\n');
res.end();
}
यह पूरी तरह से काम किया। धन्यवाद! –
हालांकि, यह एक पुराना उत्तर है, इसे काम करने के लिए आपको 'var proxy = new httpProxy.RoutingProxy(); ' ' var proxy = httpProxy.createProxyServer ({}) के साथ प्रतिस्थापित करना चाहिए;' – Saber