Knowledge Base/Engine Yard Cloud Documentation/Deploy your Application

Deploy Your Node.js Application on Engine Yard Cloud

Keri Meredith
posted this on September 10, 2012 10:34 AM

Updated: March 7th, 2013

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"
}
}

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.

    Node_chat_app.png

  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.

 

Comments

User photo
Paul Sutcliffe
resunate

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")

April 05, 2013 11:50 AM