Heroku + node.js: I have a server which uses multiple ports. How can I get Heroku to allocate them?

前端 未结 3 566
长情又很酷
长情又很酷 2020-12-05 07:05

Umm I\'ll try to be more clear..

In an application server I have written in node.js, I have inner-proxy for multiple ports:

  • in my 8080 por
3条回答
  •  独厮守ぢ
    2020-12-05 07:44

    I know this is an old post, but I wanted to provide an up-to-date response for reference and future use:

    If you're using socket-io, binding to the same port is easy. Other websocket libs should have a similar approach (from https://github.com/socketio/socket.io#how-to-use):

    In conjunction with Express Starting with 3.0, express applications have become request handler functions that you pass to http or http Server instances. You need to pass the Server to socket.io, and not the express application function. Also make sure to call .listen on the server, not the app.

    const app = require('express')();
    const server = require('http').createServer(app);
    const io = require('socket.io')(server);
    io.on('connection', () => { /* … */ });
    server.listen(3000);
    

    You'll now have http & ws traffic flowing through a single port (Heroku doesn't route http/tcp separately, if it did your websockets wouldn't work period).

    I prefer this method due to environment parity & testing, i.e. no need to setup subdomains or port routing

提交回复
热议问题