Tuesday 10 April 2012

Hello Node: Getting Started With Node.js on OS X

The following post will show you how to get up and running with Node.js on OS X. It will take you through the steps of installing all the components needed for creating a basic "Hello World" application.

What is Node.js

Before we get started I think we should first look at what Node.js actually is; this is from the Node.js website:

Node.js is a platform built on Chrome's JavaScript runtime for easily building fast, scalable network applications. Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient, perfect for data-intensive real-time applications that run across distributed devices.

Basically we can look at Node.js as an alternative to PHP, ASP.NET or Ruby on Rails.

Node.js is becoming increasingly popular and looks set to grow exponentially in 2012. Microsoft has recently announced that Windows Azure will allow Node.js applications to be hosted and run on their servers. A number of web applications are also using Node.js such as Cloud9 IDE and Trello.

Installing Node.js

The first stage in using Node.js is installing Node.js!

You can either download the installer package from the Node.js website here. Or if you use homebrew (which I recommend you do) then you can run the following command from the prompt:

$ brew install node

Hello World

Now we've got Node.js installed let's make the basic "Hello World" application. The first step is to create a a directory to store the files needed for this application. With this example we only need one file, so load up your favourite text editor and enter the following (this is the basic example from the Node.js website):

Save this file as 'example.js'.

Now we have the code let’s run it. To do this, from the command prompt, type:

% node example.js

This starts a Node.js server, go to 'http://127.0.0.1:1337/' in your browser and you will see it running.

Let's go further

Now we've got a very basic "Hello World" application up and running let’s go a bit further and look at some of the third party libraries out there to help us.

NPM

The first thing any Node.js developer needs is NPM. Think of this as RubyGems or Nuget. It can be installed through a simple command:

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

Express

Express is server-side web development framework built on Node.js. It is the most popular Node.js framework out there at the moment. Installing it is easy using NPM, we have two options:

$ npm install express

$ npm install -g express

I recommend using the second option. The `-g` flag creates a global executable version so you don't need to keep installing it for each Node.js application you write.

Now we have Express installed we can create a new application. Express does a lot of the hard work for you. The following command is all you needed:

$ express /tmp/foo && cd /tmp/foo

Then you need to installed the dependancies:

$ npm install -d

Finally start the server:

$ node app.js

The server should now have started on: http://localhost:3000/.

Making Changes

Let’s make a simple change to the application; instead of saying "Express" as the the title lets say "Hello Node Express". This is a simple change, inside your application folder you will see a sub-folder called "routes" and inside this folder a file called "index.js". Open this in your text editor. Here you will see the controller that handles returning the homepage. Without going into too much detail, change the title property on line 7 to say "Hello Node Express". If you still have your Node.js running, stop and start it again (control-C stops the server from the prompt). Refreshing the page should show the changes.

It’s a pain to keep stopping and starting the Node.js server every time you make a change. Luckily there is a package that solves this, Node Supervisor. Again like most libraries in Node.js this can be installed via NPM using:

$ npm install supervisor -g

Now, rather than using the `node` command to start the Node.js server we use the `supervisor` command:

$ supervisor app.js

Now any changes made to "index.js" or any other file within the application you will no longer need to restart the server.

Conclusion

As you can see it is relatively simple to get Node.js up and running. Hopefully you now have a greater understanding on getting started and the basics needed to get going.

Happy coding.

No comments:

Post a Comment