Node.js(Part -1) : Getting started

As promised, I now begin the series of tutorials on the amazing new technology called Node.js which I have started working on some time back. Though I don’t promise that the methods I suggest are the best way to achieve the required goal, but I can guarantee that they work.

To start with, I assume that the reader at least knows what node.js is about. To quote the official node website Node.js – Node is an “Event-driven I/O server-side JavaScript environment based on V8.”

There… thats it. In its entirety, node has a minimalistic core which provides it the amazing flexibility to be extended. There are many amazing features in node that you can read about in its official documentation.

Before you proceed, I strongly recommend you read THIS tutorial and TRY all the examples given in it to get the idea of how node works with asynchronous callbacks. Though it would be better if you did the whole thing step by step yourself, below is the link to my github profile where you can download the completed example 😛 .

https://github.com/ric03uec/nodejsTest

In this series, I will be emphasizing on building a full fledged web-application with following features:

  1. Basic design of an application

  2. Creating an MVC architecture using Express framework

  3. Using Twitter Bootstrap to get better looking widgets with JADE templating engine

  4. User authentication, both custom and using services like twitter, facebook, google etc.

  5. Sending emails from the node application

… and anything I think I found some difficulty looking up and is worth mentioning.

Without further ado, lets get to it.

To proceed further you should have Node and NPM (Node Package Manager) installed. All the examples have been tested on Ubuntu 11.10.

– Using npm, install expressjs globally by issuing following command

After this is installed, you can create an example application called ‘myblog’ by just punching in following command

Express will create a directory called ‘myblog’ and a bunch of files in it. You dont need to know what exactly those are there for right now. I will be explaining that later.

Now navigate into the ‘myblog’ folder and type in

You will find some error saying – ‘Cannot find module ‘express’. Right. But you just did create this very folder by using express only. Turns out that this application requires express locally and hence you have to install express locally for this module also. This is true for any node module. The dependencies have to be satisfied locally.  Ok, so what do you do now? Take a look at ‘package.json’ file.

This file contains all the dependencies that your application requires and it will have two entries – express and jade. Now just type in:

After waiting for not-so-long , you  will see that something has happened. What ‘npm’ has done is, it has read the dependencies specified in ‘package.json’ file and installed all those dependencies locally in your application. You could’ve done it manually by specifying the module names after npm install command like

After installation, all these dependencies (henceforth called modules) will be installed in the newly created directory – node_modules.

Now again type in following command

The console will tell you that the server is running on port 3000 on localhost. Just type in ‘http://localhost:3000″ on your browser and voila… you have just made and run your first nodejs application. Though it sucks as of now, I will tell you how to make it better.

That’s it for this session. Try to look into the automatically created folders and figure out why those things are there. I will get to it in the next post. And again. DO go through the link I mentioned in the beginning of this post. That will help you GREATLY in understanding things further…

Share Comments
comments powered by Disqus