Deploy Your Node.js Application on Engine Yard Cloud

Updated:

Node.js is a fast growing development framework for high performance, highly scalable Web applications. Built on JavaScript — the language of the browser — Node.js helps to unify development of front-end and server-side architectures.

This page describes how to create a Node.js application and deploy it on Engine Yard Cloud:

Note: If you already have a Node.js application in a git repository or if you want to deploy one of the Engine Yard test applications for Node.js, skip to Deploy your Node.js application on Engine Yard Cloud.

Additional information at the bottom of this page:

Prepare your local machine for Node.js development

If you haven't written Node.js applications before, follow these steps to set up your local machine for Node.js development.

To prepare your local machine for Node.js

  1. Install Node.js.

  2. Install npm (needed only if you install via brew, otherwise included in node itself).

    Npm is the default package manager for Node.js.

    curl http://npmjs.org/install.sh | sh 

Create your Node.js application

Write and test your Node.js application on your local machine. The two important files are the package.json file and the app.js or server.js file.

To create your Node.js application

  1. Specify your application dependencies in package.json.

    This file is used to resolve the dependencies that your application needs. For more information about the package.json file, see Specifics of npm’s package.json handling. The sample package.json file for the node-simple-example application is shown below.

  2. Write your application in app.js or server.js.
    If you don’t have an application, try the node-simple-example application.

  3. Install the dependencies for your application by running npm install:

    npm install 

    For example, this installs Express if you are using the node-simple-example application.

  4. Test your application locally.

  5. Push your application files to a git repository.

The node-simple-example application

This is the application for the github.com/engineyard/node-simple-example. This application is very similar to the Express example. One modification is that the port that the application is running on is set using the environment variable PORT.

var express = require(‘express’); 
var app = express.createServer();
var port = process.env.PORT || 3000;
app.get(‘/’, function(request, response) {
response.send(‘Hello Engine Yard Cloud!’);
});
app.listen(port);


This is the example package.json file for the node-simple-example application:

{ 
"name": "node-hello-world",
"version": "0.0.1",
"dependencies": {
"express": "2.5.0"
}
}

Set your Node.js Version Number

You need to set the Node.js version number when deploying a new application or upgrading one.

To set the Node.js version number:

  1. Edit the following entry in the package.json file for your application:

    {"engines": {"node": ...} }

    You can use greater than (>), less than (<), and equal to signs to specify a range of version numbers. If you use "x" to represent the last digit (for example 0.8.x), then you will be given the most recent 0.8 version that is available. If you do not specify the version (or if you specify "*" as the version), then any Node version is acceptable.

    Note: Node.js versions 0.10.28, 0.10.38 and 0.12.4 are ONLY available on our Gentoo 12.11 stack.

    For Gentoo 12.11, see Engine Yard Gentoo 12.11 Technology Stack for all the supported versions.

    For Gentoo 2009, see Engine Yard Gentoo 2009 Technology Stack for all the supported versions.

  2. If you are upgrading Node.js, then you must redeploy your application to use the new version.

Deploy your Node.js application on Engine Yard Cloud

These steps describe how to deploy a Node.js application (from a git repository) on Engine Yard Cloud.

To run a Node.js application on Engine Yard Cloud

  1. In the dashboard, create a new application and set Application Type to Node.js.
    If you do not have a Node.js application in a git repository, try the Node.js Chat app.

    create_app_window.pngE

  2. Create the app environment.

    The application server stack defaults to Nginx, which proxies dynamic HTTP to Node.js.

    (Optional) If you need to use WebSockets, select Node.js, which serves all TCP and HTTP requests and allows WebSockets. (The sample Node.js Chat app uses WebSockets.) See Using WebSockets for more information.

  3. Create a custom cluster.
    Set the Server Size to Large for the application instances.

  4. Deploy your application.

Environment variables

By default, Engine Yard Cloud exposes some environment variables to Node.js applications. These variables can be used to configure applications.

The variables are:

  • PORT: The port number where the Node.js server is running.
  • NODE_ENV: The environment in which the application is running.
  • NODE_ROOT: The root path of the application.
  • DB_USER: User name to connect to the database.
  • DB_PASSWORD: Password to connect to the database.
  • DB_HOST: Database host server.
  • DB_MASTER: Alias for the database host server.
  • DB_SLAVES: List of slaves host names if there are any.

These variables can be accessed through the process. For example, to get the port number:

 var port = process.env.PORT; 

Using WebSockets

Engine Yard Cloud uses a reverse TCP proxy for Nginx that routes WebSocket requests directly to your Node.js application. You can use frameworks like Socket IO to work with WebSockets.

This repository contains an example Node.js application that uses Socket IO to create a chat room: github.com/engineyard/chat

About CoffeeScript and Node.js

You can write your Node.js application in CoffeeScript instead of JavaScript. Files named app.coffee and server.coffee are ignored if there are corresponding app.js and server.js files.

More information 

For more information about...See...
Developer notes from the Engine Yard blog                                                      

Getting Started with Node.js
Quickstart: How to run Node.js on Engine Yard Cloud
A Time and a Place: When To Use Node.js

Node.js in general                                                                 http://nodejs.org/                     
NodeTuts, like RailsCasts                                                               http://nodetuts.com/                     
Node.js Toolbox, like Ruby Toolbox                                                                 http://nodetoolbox.com                     
How to access configuration variables within a Node app (On the Ruby side we have the ey_config gem. What is the equivalent for Node.js?)                 https://npmjs.org/package/ey_config                     
Video introduction: "Node.js Introduction on Engine Yard Cloud"                                                     Node.js Introduction on Engine Yard Cloud          

If you have feedback or questions about this page, add a comment below. If you need help, submit a ticket with Engine Yard Support.

Was this article helpful?
0 out of 0 found this helpful
Have more questions? Submit a request

Comments

  • Avatar
    Paul Sutcliffe

    I had my app in server.coffee, but to get it to work, I had to have an app.js file that included: require("./server.coffee")