Just ten commands or fewer will get you started, showing you how to write a “Hello World” program using Node.js on our optimised, pre-installed image of Ubuntu 13.04.
If you want to use a different Linux distribution, most of this tutorial will still be correct. You will have to translate all of the apt-get statements and package names to your distribution of choice. Configuration file paths and layouts may also differ.
If you would like more information about how to set up a Linux VM using our system, please visit our other tutorials.
Step 1: Install Node Package Manager
First, fire up a fresh terminal and ensure your package lists are up-to-date:
sudo apt-get update
First you should install git, so that we can then install NVM, or Node Version Manager.
sudo apt-get install git
Since NodeJS is so new, version control can otherwise be a nightmare. NVM will help keep track of those constant updates and varying versions. This method uses an easy install script:
curl https://raw.github.com/creationix/nvm/master/install.sh | sh
If you’ve been successful, you should be prompted to restart your terminal. Now you’ve added to the path in ~/.profile, you need to update it:
Now let’s type the command suggested when you ran that install script:
This throws you a big, long list of all the available NodeJS versions. At the time of writing this tutorial, we’re up to v.0.11.10, so let’s install that one:
nvm install 0.11.10
Step 2: Install Express web server
Express is a minimal yet very flexible HTTP server framework. Install it using:
npm install -g express
Express has a very simple way of creating all the necessary tools and files for a simple web app. To make an app called “helloworld” you simply write:
Now move to that fresh directory:
Now we need to get all the necessary dependencies. The command for this is:
You can now test the app by running it with node. By default it is called app.js:
The Express server should now reply that it is listening on port 3000. This is default behaviour, it can be changed within app.js, but for now, let’s just check it’s working. Point your browser to the IP address of your VM (visible from your Elastichosts control panel) on port 3000. Your url should look like this:
Step 3: Configure your Express web server
Now we can edit the file using your favourite text editor. The simplest is probably nano:
app.set('port', process.env.PORT || 80);
Now, if we want to operate like a more familiar web server, we need to edit the index.jade file. You can find this in the “view” directory of your project, so the command should look like this:
The file itself isn’t written in standard html, but rather a templating language called Jade. Aside from the templating conventions, you can now edit this file as you would index.html:
extends layout block content h1 Hello p Hello world, I'm running NodeJS on an Elastichosts VM!
Step 4: Bask in the glory of your own awesomeness
Now just navigate to http://YOUR_IP_ADDRESS once again to see your Node.js application in action!