node-http-proxy
node-http-proxy copied to clipboard
I want to either proxy or mock based on request body.
In the req.on('end", ... ), proxy.web does not work. I want to either proxy or mock based on request body.
var proxy = httpProxy.createProxyServer({});
...
var body = [];
req.on('data', function(chunk) {
body.push(chunk);
}).on('end', function() {
body = Buffer.concat(body).toString();
// At this point, we have the headers, method, url and body, and can now
// do whatever we need to in order to respond to this request.
if(req.url.indexOf("/something") >= 0) {
var a = JSON.parse(body);
if(a.id == "testid") {
console.log("mock");
res.writeHead(200);
res.end("predefined response");
return;
}
}
console.log("proxy");
proxy.web(req, res, { forward: 'http://real_url' });
});