简体   繁体   English

使用http-proxy对新端口的代理请求

[英]Proxy request to new port with http-proxy

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 我使用这个代码我想创建代理,所有应用程序调用端口3000将被“引导”到端口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 现在,当我使用原始端口(3000)运行应用程序时,我在浏览器中看到了

request successfully proxied to 3002 请求成功代理到3002

  • When I change the port (in the browser ) to 3002 I still get the same message,why ? 当我将端口(在浏览器中)更改为3002时,我仍然收到相同的消息,为什么? 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 ? res.end()是否也应该存在?

I use the code from https://github.com/nodejitsu/node-http-proxy 我使用https://github.com/nodejitsu/node-http-proxy中的代码

When I change the port (in the browser ) to 3002 I still get the same message,why ? 当我将端口(在浏览器中)更改为3002时,我仍然收到相同的消息,为什么? 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. 您设置了一个侦听端口3002的“真实”服务器。如果您尝试直接访问它,则没有理由不这样做,并且您的请求事件处理程序(在该服务器上)所做的是返回字符串“请求成功代理到:“+网址。 Nothing special to see here :-) 这里没什么特别的:-)

What should I put in production inside the second create server ? 我应该在第二个创建服务器中生产什么?
Does the res.end() should also be there res.end()是否也应该存在

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. res.end()是否应该存在是服务器的功能。 So again, nothing to see here :-) 再说一次,没有什么可看的:-)

If you want to see a close real scenary try adding a hostname in the listen funciton: 如果你想看到一个真实的风景,试着在listen功能中添加一个主机名:

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

and then try to connect from other computer (or with the same computer but using your network IP). 然后尝试从其他计算机(或与同一台计算机,但使用您的网络IP)连接。 If your net IP is 192.12.13.14 try: 如果您的网络IP是192.12.13.14,请尝试:

http://192.12.13.14:3002 

and

http://192.12.13.14:3000

to see the diference 看到差异

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM