Node.js on Azure gotchas

October 15, 2014

Two gotchas that “gotch” me when deploying a Node.js app on Azure:

1: Azure isn’t able to start your app

Azure starts your app using npm start. Try npm start locally before you deploy to Azure, and if doesn’t work (exits silently without starting your app) it’s possibly because node server.js wouldn’t start your app either!  That’s all npm start does by default.

You can rename to server.js to match convention, or you can specify an alternative start script in package.json:

  “scripts”: {“start”:”node <name of your script here>”}

for example for snowflake.js

  “scripts”: {“start”:”node snowflake.js”}

2: You aren’t letting Azure specify port, or you think you are, but you copy-pasted buggy sample code

Azure sets environment variable “PORT” to tell your app what port to HTTP .listen() on. It does this so it can forward requests from 80 and 443 to an expected port. So you’ll need to write code to pick that environment variable value up and .listen() on it.

Example code typically picks up the environment variable like this, providing a default of 1337:

var port = process.env.PORT || 1337;

But I’ve also seen a buggy sample code the Internet:

var port = process.env.PORT | 1337; // DON’T DO THIS, THIS IS BITWISE OR

With the buggy sample if Azure wants your app to listen on 8000, it’ll listen on 8057. Oops.