1

有一个使用 geddy 框架实现的现有 node.js 应用程序,它由 Heroku 的工头启动,如下所示:

web: geddy

我正在努力使其成为 Heroku 附加组件。Heroku 有一种方法可以自动生成附加组件所需的骨架代码,但它是使用 express 实现的。它由以下命令启动:

web: node web.js

在内部,Heroku 只为每个应用程序分配 1 个端口(将外部流量路由到它)。有没有办法在同一个端口上同时启动现有的 geddy 应用程序和 add-on express 应用程序?或者有某种类型的应用程序级路由器,可以根据传入的请求路径转发到 geddy 或 express?

4

1 回答 1

1

假设您在 Heroku 上并且仅限于 Node.js 应用程序,我建议您立即启动一个新节点作为反向代理。一个快速而肮脏的例子如下:

代理.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);

第一个.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);

第二个.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);

三个脚本都用node启动,测试结果如下:

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

这正是您所需要的:让两个应用程序看起来像是在同一个端口上监听的东西。有关更多详细信息,请查看node http-proxy 模块

于 2013-12-15T10:33:50.670 回答