proxy node request to new port and act like reverse proxy
Asked Answered
A

2

17

I need to create an application that proxies a request from port A to Port B. For instance if a user connects on port 3000 he will be routed (under the hood) to port 3001, therefore the "original" application will run on port 3001 but in the client (browser) the user will put port 3000. Not redirect...

http://example.com:3000/foo/bar

A new server will be created which listens to port 3001 and all the call are actually to port 3000 running with the new server and new port. Since port 3000 is actually occupied,by my reverse proxy app? how should I test it...

Is there a way to test this to verify that this is working,e.g. by unit testing?

I've found this module https://github.com/nodejitsu/node-http-proxy which might be able to help.

Auriol answered 18/7, 2015 at 14:6 Comment(8)
what module did you find? What did you try so far to implement this?Geniality
@doldt- :) Thanks I've added the module nameAuriol
node-http-proxy is good solution. why You're unsure? (:Muire
@num8er-Im not sure how to test it and see that this is actually working...There is also a lot of option there so which one to choose...Auriol
Do you have the option to use something like nginx or HAproxy rather than a node server? They are designed to manage this sort of thing.Escharotic
@JacobTomlinson- Thanks Im not familiar with nginx and HAproxy,If I can use it as node module and make it work this can be option...Auriol
They're separate software, but if you're willing to use them (and validate your configuration) they're widely used and very reliable.Foregoing
As @Foregoing said they are nothing to do with node. But they are generally accepted as the software you use for proxying requests.Escharotic
I
8

Straight from the node-http-proxy docs, this is quite simple. You can test it simply by making an HTTP request to port 3000 -- if you get the same response as you do on port 3001, it's working:

var http = require('http'),
    httpProxy = require('http-proxy');

//
// Create a proxy server with custom application logic
//
var proxy = httpProxy.createProxyServer({});

var server = http.createServer(function(req, res) {
  // You can define here your custom logic to handle the request
  // and then proxy the request.
  proxy.web(req, res, {
    // Your real Node app
    target: 'http://127.0.0.1:3001'
  });
});

console.log("proxy listening on port 3000")
server.listen(3000);

I highly recommend you write a suite of integration tests using some thing like for your project as well - in this way, you can run your tests both against your server directly and against your proxy. If tests pass against both, then you can be assured your proxy is behaving as expected.

A unit test using and would look something like this:

var should = require('should');

describe('server', function() {
    it('should respond', function(done) {
                                 // ^ optional synchronous callback
        request.get({
            url: "http://locahost:3000"
                                // ^ Port of your proxy
        }, function(e, r, body) {
            if (e)
                throw new Error(e);
            body.result.should.equal("It works!");
            done(); // call the optional synchronous callback
        });
    });
});

You then simply run your test (once Mocha is installed):

$ mocha path/to/your/test.js
Inexpensive answered 24/7, 2015 at 17:13 Comment(7)
Thanks but I already saw this logic in the link which I provided in the post :), my question is how actually I can test/simulate it...(maybe with super test) , I'll try to make long story short :) I need to develop this proxy and I integrate it with another component in few weeks from now and I need by some test to verify that concept is working ... since the original port is occupied its tricky...I want to isolate this code and test it somehow...can you assist please?Auriol
Thanks but how can I do that can you please provide an example how to test this scenario ?Auriol
Thanks but this test is not sufficient to verify this is working... :),where is the place which I route the call to other port ?the create servers ...This is the problem which I faceAuriol
I'm confused here. You put a proxy in front of your server and you run a test against it. If your service on port 3001 responds and your test passed, it works.Inexpensive
To clarify, your test is run against the proxy. Proxies, by nature, should respond identically to a call made against the native application. Tests written for the native app, when pointed at the proxy, should behave identically. If they do, success. If they do not, failure.Inexpensive
Thanks,last question :) how would you extend the unit test if you need to call it from express ,Lest assume that express is listen to specific path and it this is called it create the reverse proxy serverAuriol
No, because you need something to automate the indication of tests. You should construct each call with some thought and care to ensure it accurately tests what your should respond with.Inexpensive
S
3

You can either verify that this is working by adding the following to the proxy request (as described in Remus answer )

proxy.on('proxyReq', function (proxyReq, req, res, options) {
    res.setHeader('App-Proxy', 'proxy');
});

In this way you can verify that your "original" call is working against the new server proxy and even provide the ability to create a UT,in addition you can use the changeOrigin:true property...

Sherasherar answered 26/7, 2015 at 19:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.