Proxy request to new port with http-proxy
Asked Answered
I

2

6

I use this code I want to create proxy that all the application calls to port 3000 will be routed "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 run the application with original port(3000) I see in the browser

request successfully proxied to 3002

  • When I change the port (in the browser ) to 3002 I still get the same message,why ? is it OK?

  • What should I put in production inside the second create server ? I mean instead of the

    res.writeHead(200, { 'Content-Type': 'text/plain' });
    res.write('request successfully proxied to: ' + req.url + '\n' + JSON.stringify(req.headers, true, 2));
    res.end();
    
  • Does the res.end() should also be there ?

I use the code from https://github.com/nodejitsu/node-http-proxy

Immiscible answered 10/8, 2015 at 6:43 Comment(0)
T
4

When I change the port (in the browser ) to 3002 I still get the same message,why ? is it OK?

That's perfectly fine. You set up a "real" server listening on port 3002. If you try to access it directly there's no reason for that not to work, and what your request event handler (on that server) does is return the string "request successfully proxied to: " + the url. Nothing special to see here :-)

What should I put in production inside the second create server ?
Does the res.end() should also be there

You should put some real, useful server logic. You didn't describe what your server does, and I don't think it's relevant to the question that you do. Whether res.end() should be there or not is a function of what the server does. So again, nothing to see here :-)

Thurmond answered 10/8, 2015 at 6:50 Comment(8)
Thanks Amit 1+, regard the second ...I just want to proxy all the request from port 3000 to 3002 that it! , so what should I put in the second server creation ?just res.end()?Immiscible
The point in creating a (reverse) proxy is to forward/hide/LB/other requests coming in through one endpoint to a real server on another endpoint (usually internal). In such cases, you don't need to create the second server at all. It should already "be there", doing something useful.Thurmond
Thanks Amit, so can you provide in the post how exactly its should look like assume that I only need to use just proxy from 3000 to 3002 ,How would you write it (in prod) since I'm fairly new to this topic, Thanks in advance sir!Immiscible
What you did is exactly fine for demonstation purposes. In real production you won't have the 2nd createServer at all.Thurmond
Ok so I just new to this topic and copy it from the link just provided ,can you please show how exactly I should use it?Immiscible
Respectfully, I don't think you know what a reverse proxy is, or what it's used for, try watching this introductory video and also read a bit about it (Google: reverse proxy)Thurmond
So what do you say that the code which I've posted is not reverse proxy ? AFAIK change the ports "under the hood" is a reverse proxy...Can you provide example or this is too complicated ?Immiscible
It IS a reverse proxy, but there's no real server you're proxying to. The point is to forward request to a real server. You don't have a real server, nor is that important for the purpose of proxying. Really, try to read a little bit about it.Thurmond
A
1

If you want to see a close real scenary try adding a hostname in the listen funciton:

}).listen(3002,'127.0.0.1');

and then try to connect from other computer (or with the same computer but using your network IP). If your net IP is 192.12.13.14 try:

http://192.12.13.14:3002 

and

http://192.12.13.14:3000

to see the diference

Aerospace answered 12/8, 2015 at 21:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.