【问题标题】:How to run express and geddy apps on the same port?如何在同一个端口上运行 express 和 geddy 应用程序?
【发布时间】:2013-12-15 18:33:50
【问题描述】:

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

web: geddy

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

web: node web.js

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

【问题讨论】:

    标签: node.js heroku express geddy


    【解决方案1】:

    假设您在 Heroku 上并且仅限于 Node.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

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

    三个脚本都用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 module

    【讨论】:

      猜你喜欢
      • 2012-06-28
      • 2015-01-07
      • 1970-01-01
      • 2023-01-19
      • 2015-11-07
      • 1970-01-01
      • 2017-11-04
      • 2019-06-15
      • 1970-01-01
      相关资源
      最近更新 更多