How to deploy Node.js application to the (Nodester) cloud

Tags: javascript, node-js, nodester, cloud, paas

Building and running Node.js applications locally is really easy, but the time comes to deploy the application. One of the most popular and easy solution are PaaS cloud hosting services, but their environments require some changes being made to your application. I've just gone through that process last week with Nodester, and here's what needs to be done to deploy successfully to the cloud...

Cloud runtime environments

Compared to the local development environment, which runs your application on the port you specify when starting the node server (usually the default 3000), PaaS cloud environment works differently. Although you will get a public URL to access your application through the standard web port 80, Node applications in the cloud are not run on port 80 - there's a proxy service which will map that transparently.

Instead, the cloud environment will assign a random port to your application when you create the application container.

What you need to is bind your application to listen to the assigned port. Of course, that port is different for different instances of your applications, for different providers, and you'll get a different port if for any reason you destroy and recreate your application.

Leveraging environment variables to gain application runtime information

What you really don't want is changing port manually on each (re)deployment, nor tracking those changes through your repository. It would be an inappropriate way of handling things, you'll agree.

What you need is to leverage the application runtime information data that is stored in your application container as environment variables. Minimum you will get are variables containing host and port information for your application, which you can use instead of hardcoding it in your application.

Preparing your code to handle port changes transparently

Let's now show the changes that need to be made. Your original Node application will usually have something along these lines (this example additionally uses Express.js web framework on top of Node):

var express = require('express') 
  , app = express.createServer();    // initializes express.js app 

What you need to do is to fetch the port assigned to the application, and tell the application to listen at that port. Also, you'd want the application to have a fallback to your expected port while in development environment (where no environment variables are set).

For Nodester service, the application port environment variable is called app_port, so you'll rework your application to the following:

var express = require('express') 
  , appPort = process.env['app_port'] || 3000
  , app = express.createServer();

If you're new in Javascript, the line appPort = process.env['app_port'] || 3000 means if environment variable app_port is set, assign its value to the appPort variable, or assign 3000 if environment variable is not set (doesn't exist).

Publishing the application

The application code prepared this way is now ready to be git-pushed to your cloud hosting service. If everything goes well, application will be available at its public URL within seconds.

A full Javascript stack demo application published this way can be checked out at


While there are several PaaS hosting options for Node.js applications today, a great free and open source Node.JS hosting platform and service, Nodester is used in this example. If you got interested, when you manage to get your access coupon, also check the getting started help and FAQs.

You may also check out how the full Express.js application initializer, with the above logic implemented, looks like.

Moving to cloud deployments introduces some new rules, but also some neat tricks are available to adapt your apps to it. Enjoy your cloud-surfing.

Story comments:

blog comments powered by Disqus