I am using this code. I want to create a proxy so that all application calls on port 3000 are redirected "under the hood" to port 3002
var http = require('http'), httpProxy = require('http-proxy'); var proxy = httpProxy.createProxyServer(); http.createServer(function (req, res) { proxy.web(req, res, { target: 'http://localhost:3002' }); }).listen(3000); // Create target server http.createServer(function (req, res) { res.writeHead(200, { 'Content-Type': 'text/plain' }); res.write('request successfully proxied to: ' + req.url + '\n' + JSON.stringify(req.headers, true, 2)); res.end(); }).listen(3002);
Now, when I launch the application with the source port (3000), I see in the browser
request is successfully proxied to 3002
When I change the port (in the browser) to 3002 , I still get the same message, why? this is normal?
What should I add to production inside the second server? I mean instead
res.writeHead(200, { 'Content-Type': 'text/plain' }); res.write('request successfully proxied to: ' + req.url + '\n' + JSON.stringify(req.headers, true, 2)); res.end();
Should there be res.end() ?
I am using the code from https://github.com/nodejitsu/node-http-proxy
source share