Running your node.js express web apps in port 80

You use node.js and express framework - you have made great choice! By default, express application will run on port 3000, and it is fine. In production environment, you may want to use nginx as a forward proxy (and I will write next post about it) but another way is to run your app in port 80.

Assuming that your app.js already contains following:

app.configure(function(){
    app.set('port', process.env.PORT || 3000);
    ...
});

In shell, you can type following command:

PORT=80 node app.js

or if you use forever, then:

PORT=80 forever start app.js

On your OSX you may get following warning:
TypeError: Cannot read property 'getsockname' of undefined

The cause for this is general problem - you need to have root privileges in order to run applications in port lower than 1024. To avoid this, you can type

sudo PORT=80 node app.js

and your application will work normally in port 80. Another common mistake is that you may still have Apache running on your OSX, then typing sudo apachectl stop before launching your node app will solve the problem.

Happy coding in node.js and express!

Popular posts from this blog

Enable HTTP/2 Support in AWS ELB

How to enable SSL on NGINX running on Amazon Linux