node-http-proxy icon indicating copy to clipboard operation
node-http-proxy copied to clipboard

I want to either proxy or mock based on request body.

Open anyone-can-test opened this issue 10 years ago • 0 comments

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' });

       });

anyone-can-test avatar Oct 12 '16 06:10 anyone-can-test