Getting Started with Node.js

Requirements

Before doing this tutorial you should have setup your environment:

All the operation achieved with the 'scalingo' CLI can also be done on our web dashbaord.

Initialize your application

$ mkdir nodejs-app
$ cd nodejs-app
$ npm init

#
# You need to fill the different info field for your project
#

$ npm install express --save
$ echo "node_modules" > .gitignore

Write a base server file

server.js

// Node.js - Express Sample application

var express = require('express')
var app = express()

app.get('/', function (req, res) {
  res.send('Hello World!')
})

var server = app.listen(process.env.PORT || 3000, function () {
  var host = server.address().address
  var port = server.address().port
  console.log('App listening at http://%s:%s', host, port)
})
The `process.env.PORT` will read the environment variable PORT dynamically provided by our container manager.

Define how to start your application

The ‘Procfile’ (with a capital ‘P’) is the file defining how your application is supposed to start (more info about Procfile), here is the content you have to write in the ‘Procfile’ file for this project:

web: node server.js

Commit your application

$ git init
$ git add .
$ git commit -m "Base Node.js application"

Create your application on Scalingo and deploy

This operation has to be done in the directory of your project.

$ scalingo create nodejs-app
Git repository detected: remote scalingo added
→ 'git push scalingo master' to deploy your app

$ git push scalingo master

Access your application

…
Waiting for your application to boot...
<-- https://nodejs-app.scalingo.io -->

How to keep your application alive

For Node.js apps we recommend to use a tool such as ​forever​ https://github.com/foreverjs/forever.

Now develop your project and build something amazing!

schedule 09 Feb 2015