Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

node.js

heroku

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 ');
    console.log(request);

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

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

    path.exists(filePath, function(exists) {

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

 }).listen(5000);

 console.log('Server running at http://127.0.0.1:5000/');

Any idea ?

like image 782
user428900 Avatar asked Mar 28 '13 22:03

user428900


People also ask

What is 503 error heroku?

Whenever your app experiences an error, Heroku will return a standard error page with the HTTP status code 503. To help you debug the underlying error, however, the platform will also add custom error information to your logs.

Why is my node js app crashing with an R10 error?

R10 - Boot timeout errors occur when a web process took longer than 60 seconds to bind to its assigned $PORT .

What is process env port?

PORT || 3000 means: process. env. PORT means the PORT number you manually set. 3000 is the default port . If you havent set it manually then it will listen to 3000.

What port does heroku use?

Heroku expects a web application to bind its HTTP server to the port defined by the $PORT environment variable. Many frameworks default to port 8080, but can be configured to use an environment variable instead.


2 Answers

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. Important note - PORT word must be capital.

You can check out the Heroku docs on Node.js here.

like image 61
redhotvengeance Avatar answered Oct 13 '22 22:10

redhotvengeance


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
like image 81
zthomas.nc Avatar answered Oct 13 '22 23:10

zthomas.nc