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

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

web: geddy 

我正在把它变成一个Heroku插件。 Heroku有一种自动生成插件所需的框架代码的方法,但是它使用express来实现。 它由这个命令启动:

 web: node web.js 

在内部,Heroku只为每个应用程序分配1个端口(将外部stream量路由到它)。 有没有办法在同一个端口上启动现有的geddy应用程序和附加快递应用程序? 或者有某种types的应用程序级别的路由器,会根据传入的请求path转发geddy或express?

假设你在Heroku上并且仅限于Node.js应用程序,那么我build议你立即开始一个新的节点作为反向代理。 一个快速和肮脏的例子将是以下内容:

的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

 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

 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); 

使用节点启动所有三个脚本,testing结果如下:

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

这正是你所需要的:使得它看起来像两个应用程序正在侦听同一端口的东西。 有关更多详细信息,请查看节点http-proxy模块