Question

There is an existing node.js application implemented using geddy framework, it is started by Heroku's foreman like so:

web: geddy

I am working on making it into a Heroku add-on. Heroku has a way to auto-generate the skeleton code necessary for an add-on, but it is implemented using 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). Is there a way to start both existing geddy app and add-on express app on the same port? Or have some type of an application level router that would forward to geddy or express based on incoming request path?

Était-ce utile?

La solution

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. A quick and dirty example would be the following:

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

start all three scripts using node, and the test result is as follows:

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

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top