简体   繁体   English

检查在新端口上运行的应用程序

[英]Check app running on new port

I need to create application which get request for specific port and proxy it to new server on different port我需要创建应用程序来获取特定端口的请求并将其代理到不同端口上的新服务器

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例如,以下端口 3000 将代理端口 9000 并且您实际上在 9000(引擎盖下)上运行应用程序,因为客户端中的用户单击 3000

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

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

I try something like我尝试类似

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. 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.我问,因为如果我在端口 3000 中运行节点应用程序,我无法将第一个 URL 放在客户端http://localhost:3000/a/b/c 中,因为该端口已经被占用。 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);

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.我添加了一个proxyReq侦听器,它添加了一个自定义标头。 You can tell from this header if the request came from the proxy server.您可以通过此标头判断请求是否来自代理服务器。

So, if you visit http://localhost:8080/a/b/c you'll see the req.headers has a header like this:所以,如果你访问http://localhost:8080/a/b/c你会看到req.headers有一个像这样的标题:

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

This header is set only when client makes a request to 8080 port仅当客户端向 8080 端口发出请求时才设置此标头

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

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

相关问题 运行 nodejs 应用程序时端口已在使用中 - port is already in use when running nodejs app 应用程序未在 .env 文件中定义的端口上运行 | 节点JS - App not running on port defined in .env file | NodeJS 如果指定的地址(端口)在另一个应用程序上运行,window.location 不会重定向 - window.location not redirecting if address(port) specified is running on another app 与运行新React App有关的问题 - Issue related to running of New React App 是否可以检查Angular应用程序是否已经运行应用程序模块? - Is it possible to check if an Angular app already has running app modules? Express JS-使用服务器端JS检查应用程序是否在本地主机上运行 - Express JS - Using server side JS check if app is running on localhost 如何在 ionic/cordova/phonegap 中检查在前台或后台运行的应用程序 - how to check app running in foreground or background in ionic/cordova/phonegap 我的PhoneGap应用程序正在运行时,如何检查权限? - How do I check Permissions while my PhoneGap App is running? 检查我的应用程序是否已经在使用Cordova运行 - Check whether already my app is running using Cordova 创建 React App:如何让最终端口 dev-server 运行 - Create React App: how to get final port dev-server running on
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM