简体   繁体   English

如何使用Restify在同一端口上运行多个节点的应用程序?

[英]How to run multitple Node apps on the same port using Restify?

I need to run multiple Node apps on the same port. 我需要在同一端口上运行多个Node应用程序。 I've found out that I can run multiple node apps using one port, thanks to this SO question Running multiple Node (Express) apps on same port But it's not working for me probably bec. 我发现我可以使用一个端口运行多个节点应用程序,这是由于这样的问题: 在同一个端口上运行多个节点(Express)应用程序,但这可能对我不起作用。 I'm using Restify unless I did something wrong somewhere. 我正在使用Restify,除非我在某个地方做错了什么。

I already have "app1" running on this one port using PM2 built using Restify. 我已经使用使用Restify构建的PM2在端口上运行了“ app1” I've made another app "app2" . 我制作了另一个应用程序“ app2” The paths are like these: 路径如下所示:

/var/www/app1
/var/www/app2

with each app having common routes like these: 每个应用都具有如下通用路线:

app.get('/', func...);
app.get('/about', func...);
app.post('/foo', func...);
app.post('/bar', func...);

I've set up "app1" 's last lines of code as: exports.app = app instead of app.listen(8080, function() { ... }); 我将“ app1”的最后一行代码设置为: app.listen(8080, function() { ... }); exports.app = app而不是app.listen(8080, function() { ... });

and, where app is 而且, app在哪里

var app = restify.createServer({
    name: 'app1'
});

"app2" is the same as well... “ app2”也一样...

My main.js file (which is saved in /var/www/ ) is also built on Restify: 我的main.js文件(保存在/var/www/ )也是基于Restify构建的:

main
 .use('/app`', require('./app1/index').app)
.listen(8080);

where main is main在哪里

var main = restify.createServer({
    name: 'main'
});

But I'm getting an error such as this when I type node main.js (I haven't tried with PM2 yet): 但是当我输入node main.js时出现了这样的错误(我尚未尝试使用PM2):

/var/www/node_modules/restify/node_modules/assert-plus/assert.js:45
                    throw new assert.AssertionError({
                          ^
AssertionError: handler (function) is required
    at process (/var/www/node_modules/restify/lib/server.js:76:24)
    at argumentsToChain (/var/www/node_modules/restify/lib/server.js:84:13)
    at Server.use (/var/www/node_modules/restify/lib/server.js:625:6)
    at Object.<anonymous> (/var/www/main.js:47:8)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Function.Module.runMain (module.js:497:10)
    at startup (node.js:119:16)

Note: I've turned off all the apps running under PM2. 注意: 我已经关闭了在PM2下运行的所有应用程序。 There are no node apps running on any port. 任何端口上都没有运行节点应用程序。

The only way to do this effectively is to run an HTTP proxy configured to answer requests on a single port and pass them, based upon URL patterns, to servers running on other ports, a simple example of which can be found at A HTTP Proxy Server in 20 Lines of node.js Code . 有效执行此操作的唯一方法是运行配置为在单个端口上应答请求的HTTP代理,然后根据URL模式将其传递给在其他端口上运行的服务器,该示例的一个简单示例可以在HTTP代理服务器上找到。 在20行的node.js代码中

In essence, your publicly visible proxy server runs on port 80 and you run other servers to handle specific requests. 本质上,公开可见的代理服务器运行在端口80上,而您运行其他服务器来处理特定请求。

So for example, if you run three HTTP servers, one as a forwarding proxy and two for specific functions such that: 因此,例如,如果您运行三台HTTP服务器,一台作为转发代理,两台用于特定功能,例如:

  • proxy on port 80 端口80上的代理
  • server2 on port 8080 for requests matching regexp: /^\\/first(?:\\/.*)?$/ 端口8080上的server2用于与正则表达式匹配的请求:/ /^\\/first(?:\\/.*)?$/ : /^\\/first(?:\\/.*)?$/
  • server3 on port 8081 for requests matching regexp: /^\\/second(?:\\/.*)?$/ 服务器/^\\/second(?:\\/.*)?$/在端口8081上与正则表达式匹配的请求:/ /^\\/second(?:\\/.*)?$/ : /^\\/second(?:\\/.*)?$/

where the only server that has a public connection is your proxy. 唯一具有公共连接的服务器是您的代理。

When the proxy receives a request for /first or /first/index.html , it forwards the request to server2 which returns a result document that the proxy then sends back to the original requester. 当代理收到对/first/first/index.html的请求时,它将请求转发到server2,服务器2返回结果文件,然后代理将其发送回原始请求者。

When it receives a request for /second/foo/bar/page.html , it does the same but using server3 to produce a result. 当它收到对/second/foo/bar/page.html的请求时,它执行相同的操作,但是使用server3来产生结果。

http-proxy is an implementation of this strategy which uses the http-proxy-rules plugin to process and forward requests based on URL patterns. http-proxy是此策略的实现,该策略使用http-proxy-rules插件基于URL模式处理和转发请求。

UPDATE 更新

For the purposes of clarity, we assume proxy , server2 , and server3 above represent individual node HTTP server instances listening on a single IP address but separate ports on the same machine. 为了清楚起见,我们假设上面的proxyserver2server3代表侦听单个IP地址但在同一台计算机上的单独端口上的各个节点HTTP服务器实例。

Example: 例:

var http = require('http'),
    httpProxy = require('http-proxy'),
    HttpProxyRules = require('http-proxy-rules');

// Set up proxy rules instance
//   where 
//     any request for /hostname/app1 will be proxy-ed via SERVER2
//     any request for /hostname/app2 will be proxy-ed via SERVER3
var proxyRules = new HttpProxyRules({
  rules: {
    '.*/app1/': 'http://localhost:8080',  // TO SERVER2
    '.*/app2/': 'http://localhost:8081'   // TO SERVER3
  }
});

// Create reverse proxy instance
var proxy = httpProxy.createProxy();

// Create http server on hostname:80 that leverages reverse 
// proxy instance and proxy rules to proxy requests to 
// different one of two target servers
http.createServer(function(req, res) { // PROXY 

  // a match method is exposed on the proxy rules instance
  // to test a request to see if it matches against one 
  // of the specified rules
  var target = proxyRules.match(req);

  if (target) {
    return proxy.web(req, res, {
      target: target
    });
  }

  res.writeHead(500, { 'Content-Type': 'text/plain' });
  res.end('No rule found for this request');

}).listen(80);

// create a new HTTP server on localhost:8080 to process
// requests sent from the proxy
http.createServer(function (req, res) { // SERVER2
  res.writeHead(200, { 'Content-Type': 'text/plain' });
  var headers=JSON.stringify(req.headers,true,2);
  res.write('request successfully proxy-ed to SERVER2!' + '\n' + headers);
  res.end();
}).listen(8080,'localhost');

// create a new HTTP server on localhost:8081 to process
// requests sent from the proxy
http.createServer(function (req, res) { // SERVER3
  res.writeHead(200, { 'Content-Type': 'text/plain' });
  var headers=JSON.stringify(req.headers,true,2);
  res.write('request successfully proxy-ed to SERVER3!' + '\n' + headers);
  res.end();
}).listen(8081,'localhost');

Using this setup: 使用此设置:

  • only the proxy server will be available externally on port 80 仅代理服务器将在端口80外部可用
  • the servers running on ports 8080 & 8081 are only available on the local machine 在端口8080和8081上运行的服务器在本地计算机上可用
  • requests received on the proxy at hostname:80 that match the /app1 path (and descendants) will be proxy-ed by the server running on localhost:8080 在主机名:80上与/app1路径(及其后代)匹配的代理上收到的请求将由在localhost:8080上运行的服务器代理
  • requests received on the proxy at hostname:80 that match the /app2 path (and descendants) will be served by the server running on localhost:8081 在主机名:80上与/app2路径(及其后代)匹配的代理上收到的请求将由在localhost:8081上运行的服务器处理

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

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