admin管理员组文章数量:1310283
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...
: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 which might be able to help.
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.: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./nodejitsu/node-http-proxy which might be able to help.
Share Improve this question edited Jul 25, 2015 at 6:41 brandonscript 73k35 gold badges174 silver badges237 bronze badges asked Jul 18, 2015 at 14:06 user4445419user4445419 8- 1 what module did you find? What did you try so far to implement this? – doldt Commented Jul 18, 2015 at 14:18
- 1 @doldt- :) Thanks I've added the module name – user4445419 Commented Jul 18, 2015 at 14:23
- 1 node-http-proxy is good solution. why You're unsure? (: – num8er Commented Jul 18, 2015 at 14:23
- 1 @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... – user4445419 Commented Jul 18, 2015 at 14:24
- 3 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. – Jacob Tomlinson Commented Jul 24, 2015 at 12:20
2 Answers
Reset to default 8 +100Straight 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 remend you write a suite of integration tests using some thing like mocha 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 mocha and should.js 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
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...
本文标签: javascriptproxy node request to new port and act like reverse proxyStack Overflow
版权声明:本文标题:javascript - proxy node request to new port and act like reverse proxy - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741800848a2398212.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论