user428900 user428900 - 9 months ago 81
Node.js Question

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

I have my first node.js app (runs fine locally) - but I am unable to deploy it via heroku (first time w/ heroku as well). The code is below. SO doesn't let me write so much code, so I would just say that the running the code locally as well within my network shows no issue.

var http = require('http');
var fs = require('fs');
var path = require('path');

http.createServer(function (request, response) {

console.log('request starting for ');

var filePath = '.' + request.url;
if (filePath == './')
filePath = './index.html';

var extname = path.extname(filePath);
var contentType = 'text/html';
switch (extname) {
case '.js':
contentType = 'text/javascript';
case '.css':
contentType = 'text/css';

path.exists(filePath, function(exists) {

if (exists) {
fs.readFile(filePath, function(error, content) {
if (error) {
else {
response.writeHead(200, { 'Content-Type': contentType });
response.end(content, 'utf-8');
else {


console.log('Server running at');

Any idea ?

Answer Source

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.