Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
anyone-can-test opened this issue Oct 12, 2016 · 0 comments
Open

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

anyone-can-test opened this issue Oct 12, 2016 · 0 comments

Comments

@anyone-can-test
Copy link

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

       });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant