简体   繁体   English

如何在同一端口上运行Express和geddy应用程序?

[英]How to run express and geddy apps on the same port?

There is an existing node.js application implemented using geddy framework, it is started by Heroku's foreman like so: 现有一个使用geddy框架实现的node.js应用程序,它是由Heroku的领班启动的,如下所示:

web: geddy

I am working on making it into a Heroku add-on. 我正在努力使其成为Heroku附加组件。 Heroku has a way to auto-generate the skeleton code necessary for an add-on, but it is implemented using express. Heroku有一种自动生成附加组件所需的框架代码的方法,但是它是使用express实现的。 It is started by this command: 它由以下命令启动:

web: node web.js

Internally, Heroku only allocates 1 port per app (that gets external traffic routed to it). 在内部,Heroku仅为每个应用程序分配1个端口(将外部流量路由到该端口)。 Is there a way to start both existing geddy app and add-on express app on the same port? 有没有办法在同一端口上启动现有的geddy应用程序和附加Express应用程序? Or have some type of an application level router that would forward to geddy or express based on incoming request path? 还是有某种类型的应用层路由器会根据传入的请求路径转发到geddy或表示?

Assuming you are on Heroku and are limited to only Node.js apps, I would suggest you to start a new node instant as a reverse proxy. 假设您在Heroku上并且仅限于Node.js应用程序,我建议您立即启动一个新节点作为反向代理。 A quick and dirty example would be the following: 一个简单而肮脏的例子如下:

proxy.js 的proxy.js

var http = require('http'),
    httpProxy = require('http-proxy');
var options = {
  pathnameOnly: true,
  router: {
    '/foo': '127.0.0.1:8001',
    '/bar': '127.0.0.1:8002'
  }
};

var proxyServer = httpProxy.createServer(options);
proxyServer.listen(8000);

first.js first.js

var http = require('http');

http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end('I am the first server!\n');
}).listen(8001);

second.js second.js

var http = require('http');

http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end('I am the second server!\n');
}).listen(8002);

start all three scripts using node, and the test result is as follows: 使用node启动所有三个脚本,测试结果如下:

cloud@wishlist:~$ curl localhost:8000/foo
I am the first server!
cloud@wishlist:~$ curl localhost:8000/bar
I am the second server!

which is exactly what you need: something that makes it look like two apps are listening on the same port. 正是您所需要的:看起来好像两个应用在同一端口上侦听。 For more detail, look into the node http-proxy module 有关更多详细信息,请查看节点http-proxy模块

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

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