Node.js on Azure gotchas

Wade
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 because it’s not pleasing npm start

Azure starts your app using npm start. Try this command off Azure before on Azure, and if doesn’t work (exits silently without starting your app) it’s possibly because you wouldn’t run node server.js to start your app, which is what npm start does by default.

You can certainly rename to server.js to match convention, or you can specify a different script in package.json:

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

for example for my app snowflake.js

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

2: You aren’t letting Azure specify the port your app should listen on, or that code is buggy

Azure sets the environment variable “PORT” to tell your app what port it’d like it to HTTP .listen() on. It does this so it can forward requests from 80 and 443. Pick up the value Azure provides to override your choice (here, 1337) like this:

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

NOT the bitwise-or typo version I copied from somewhere:

var port = process.env.PORT | 1337;

It seems case of PORT doesn’t matter on Azure; I have also seen it port. But since case does seem to matter on my Unix dev system (export port=8888 is only reflected in process.env.port and export PORT=8888 is only reflected in process.env.PORT) I stick with the more conventional PORT.