Heroku + node.js error (Web process failed to bind to $PORT within 60 seconds of launch)
Heroku dynamically assigns your app a port, so you can't set the port to a fixed number. Heroku adds the port to the env, so you can pull it from there. Switch your listen to this:
.listen(process.env.PORT || 5000)
That way it'll still listen to port 5000 when you test locally, but it will also work on Heroku.
You can check out the Heroku docs on Node.js here.
It's worth mentioning that if your code doesn't specify a port, then it shouldn't be a web
process and probably should be a worker
process instead.
So, change your Procfile
to read (with your specific command filled in):
worker: YOUR_COMMAND
and then also run on CLI:
heroku scale worker=1
The error happens when Heroku failed to bind the port or hostname at server.listen(port, [host], [backlog], [callback])
.
What Heroku requires is .listen(process.env.PORT)
or .listen(process.env.PORT, '0.0.0.0')
So more generically, to support other environments, use this:
var server_port = process.env.YOUR_PORT || process.env.PORT || 80;
var server_host = process.env.YOUR_HOST || '0.0.0.0';
server.listen(server_port, server_host, function() {
console.log('Listening on port %d', server_port);
});