0

I know that Heroku automatically assigns a port number to your instance and doesn't work when you define a specific one like '3000'. But when I try to change my port number to make use of environment variables (like suggested in Heroku's documents and a few stack overflow answers), I always get

Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 60 seconds of launch

The app works perfectlt locally and I was wondering how best to change my code to ensure that it works on Heroku:

var express     = require('express'),
    app         = express(),
    server      = require('http').createServer(app),
    io          = require('socket.io').listen(server),
    port        = 3000,

    // hash object to save clients data,
    // { socketid: { clientid, nickname }, socketid: { ... } }
    chatClients = new Object();

server.listen(port);

Here are some of the links to things I've tried, etc:

https://devcenter.heroku.com/articles/preparing-a-codebase-for-heroku-deployment (point number 4)

Heroku + node.js error (Web process failed to bind to $PORT within 60 seconds of launch)

https://help.heroku.com/P1AVPANS/why-is-my-node-js-app-crashing-with-an-r10-error

chattyguy
  • 3
  • 1

1 Answers1

1

Just access PORT from environment variable as shown below:

var express     = require('express'),
    app         = express(),
    server      = require('http').createServer(app),
    io          = require('socket.io').listen(server),
    port        = process.env.PORT || 3000,

    // hash object to save clients data,
    // { socketid: { clientid, nickname }, socketid: { ... } }
    chatClients = new Object();

server.listen(port);
Vishal-L
  • 1,307
  • 1
  • 9
  • 16
  • I tried that already and it returned the same error. Let me know if it would help to see the entire script. – chattyguy Dec 17 '19 at 14:23