检查在新端口上运行的应用 [英] Check app running on new port

查看:130
本文介绍了检查在新端口上运行的应用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要创建一个获取特定端口请求的应用程序,并将其代理到其他端口上的新服务器

I need to create application which get request for specific port and proxy it to new server on different port

例如,以下端口3000将作为端口9000的代理 并且您实际上是在9000(幕后)上运行该应用程序的,因为客户端中的用户单击了3000

for example the following port 3000 will be proxy to port 9000 and you actually run the application on 9000 ( under the hood) since the user in the client click on 3000

http://localhost:3000/a/b/c

http://localhost:9000/a/b/c

我尝试

var proxy = httpProxy.createProxyServer({});

            http.createServer(function (req, res) {

                var hostname = req.headers.host.split(":")[0];
                var pathname = url.parse(req.url).pathname;
                proxy.web(req, res, {
                        target: 'http://' + hostname + ':' + 9000
                    });
     var proxyServer = http.createServer(function (req, res) {

                    res.end("Request received on " + 9000);
                });
                proxyServer.listen(9000);

            }).listen(3000, function () {

     });

  1. 这样做是正确的方法吗?
  2. 如何测试?我问,因为我是否在端口3000中运行节点应用程序,所以无法将第一个URL放在客户端 http://本地主机:3000/a/b/c ,因为该端口已被占用.有解决方法吗?
  1. Is the right way to do it?
  2. How can I test it? I ask since if I run the node application in port 3000 I cannot put the first URL in the client http://localhost:3000/a/b/c since this port is already taken. Is there a workaround?

推荐答案

很少有示例有关代理服务器的各种用法.这是基本代理服务器的简单示例:

There are few examples on various usage of proxy server. Here is a simple example of a basic proxy server:

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

/** PROXY SERVER **/
var proxy = httpProxy.createServer({
  target:'http://localhost:'+3000,
  changeOrigin: true
})

// add custom header by the proxy server
proxy.on('proxyReq', function(proxyReq, req, res, options) {
  proxyReq.setHeader('X-Special-Proxy-Header', 'foobar');
});

proxy.listen(8080);

/** TARGET HTTP SERVER **/
http.createServer(function (req, res) {
  res.writeHead(200, { 'Content-Type': 'text/plain' });

  //check if the request came from proxy server
  if(req.headers['x-special-proxy-header'])
    console.log('Request received from proxy server.')

  res.write('request successfully proxied to: ' + req.url + '\n' + JSON.stringify(req.headers, true, 2));
  res.end();
}).listen(3000);

要测试代理服务器是否正常工作或请求是否来自代理服务器,请执行以下操作:

我添加了一个proxyReq侦听器,该侦听器添加了一个自定义标头.您可以从此标头中判断请求是否来自代理服务器.

To Test if the proxy server working or if the request is coming from proxy server:

I've added a proxyReq listener which adds a custom header. You can tell from this header if the request came from the proxy server.

因此,如果您访问http://localhost:8080/a/b/c,您会看到req.headers具有这样的标题:

So, if you visit http://localhost:8080/a/b/c you'll see the req.headers has a header like this:

'X-Special-Proxy-Header': 'foobar'

仅当客户端向8080端口发出请求时,才设置此标头

This header is set only when client makes a request to 8080 port

但是对于http://localhost:3000/a/b/c,您不会看到这样的标头,因为客户端绕过了代理服务器,并且从未设置该标头.

But for http://localhost:3000/a/b/c, you won't see such header because client is bypassing the proxy server and that header is never set.

这篇关于检查在新端口上运行的应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆