Getting Started With Node.js Part 1: A Quick App


Node.js is a server platform built on Google Chrome’s JavaScript runtime. It has an event-driven, non-blocking I/O model so it’s a fast and efficient as you could want. It’s also probably a good glimpse into the future of server technology.

With the server running pure JavaScript, it’s a great plaform for development of JavaScript-centric applications. Using the same object across the client and server saves a lot of time and effort in wrapping up objects for transport.

Anyway, if you’re reading this, then you probably have a good idea of what Node is and why you’d want to use it. So, let’s get started.

Install Node

Node is pretty easy to get on your system. Go grab the installer and install that. Done? Easy? Okay, next.

Let’s write a server…

…without writing any code.

Rather than create a server from scratch, we’re going to use the Express Framework. This will give us a good basis from where to start.

Create new folder for your project. Open up a command terminal and navigate to there.

Node ships with a pakcage manager (NPM) than can be used to pull down all sorts of packages from the main repository. We’ll use this to get express. So, install it:

npm install express

You should see a bunch of get requests pulling down the required packages. Something like this:

$ npm install express
npm http GET https://registry.npmjs.org/express
npm http 304 https://registry.npmjs.org/express
npm http GET https://registry.npmjs.org/mkdirp/0.3.0
npm http GET https://registry.npmjs.org/mime/1.2.4
npm http GET https://registry.npmjs.org/qs
npm http GET https://registry.npmjs.org/connect
npm http 304 https://registry.npmjs.org/mkdirp/0.3.0
npm http 304 https://registry.npmjs.org/mime/1.2.4
npm http 304 https://registry.npmjs.org/qs
npm http 304 https://registry.npmjs.org/connect
npm http GET https://registry.npmjs.org/formidable
npm http 304 https://registry.npmjs.org/formidable
[email protected] ./node_modules/express
├── [email protected]
├── [email protected]
├── [email protected]
└── [email protected] ([email protected])

Now we have Express, let’s create our application. The basic command for this is just express but we’re going to use a couple of the command switches to tell it to enable session support and to use Less as the CSS engine. So, use this command:

express --sessions --css less

You’ll likely get a message telling you that the directory isn’t empty and asking if you want to continue. This is just because we have the node_modules folder in there (where Express was downloaded to) so isn’t going to be an issue. Say yes (y).

If all goes well, you’ll see something similar to:

$ express --sessions --css less
destination is not empty, continue? y
 
   create : .
   create : ./package.json
   create : ./app.js
   create : ./public
   create : ./public/images
   create : ./public/javascripts
   create : ./routes
   create : ./routes/index.js
   create : ./public/stylesheets
   create : ./public/stylesheets/style.less
   create : ./views
   create : ./views/layout.jade
   create : ./views/index.jade
 
   install dependencies:
     $ cd . && npm install
 
   run the app:
     $ node app

Express has usefully listed our next two steps for us. We need to install dependencies and then run our app.

The npm install command uses the package.json file in our application root to pull down and packages that are listed in there. So, run that:

npm install

Express will have pulled down all of the required packages for its use:

$ npm install
npm http GET https://registry.npmjs.org/jade
npm http GET https://registry.npmjs.org/express/3.0.0alpha1
npm http GET https://registry.npmjs.org/less-middleware
npm http 304 https://registry.npmjs.org/jade
npm http 304 https://registry.npmjs.org/express/3.0.0alpha1
npm http 304 https://registry.npmjs.org/less-middleware
npm http GET https://registry.npmjs.org/mkdirp
npm http GET https://registry.npmjs.org/less
npm http GET https://registry.npmjs.org/commander/0.5.2
npm http GET https://registry.npmjs.org/mime/1.2.5
npm http GET https://registry.npmjs.org/debug
npm http GET https://registry.npmjs.org/connect/2.1.2
npm http GET https://registry.npmjs.org/mkdirp/0.3.1
npm http GET https://registry.npmjs.org/mkdirp/0.3.0
npm http 304 https://registry.npmjs.org/less
npm http 304 https://registry.npmjs.org/mkdirp
npm http 304 https://registry.npmjs.org/connect/2.1.2
npm http 304 https://registry.npmjs.org/commander/0.5.2
npm http 304 https://registry.npmjs.org/mime/1.2.5
npm http 304 https://registry.npmjs.org/mkdirp/0.3.1
npm http 304 https://registry.npmjs.org/mkdirp/0.3.0
npm http 304 https://registry.npmjs.org/debug
npm http GET https://registry.npmjs.org/formidable/1.0.9
npm http GET https://registry.npmjs.org/crc/0.1.0
npm http GET https://registry.npmjs.org/qs/0.4.2
npm http GET https://registry.npmjs.org/mime/1.2.4
npm http 304 https://registry.npmjs.org/mime/1.2.4
npm http 304 https://registry.npmjs.org/crc/0.1.0
npm http 304 https://registry.npmjs.org/qs/0.4.2
npm http 304 https://registry.npmjs.org/formidable/1.0.9
[email protected] ./node_modules/jade
├── [email protected]
└── [email protected]
 
[email protected] ./node_modules/express
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
└── [email protected] ([email protected], [email protected], [email protected], [email protected])
 
[email protected] ./node_modules/less-middleware
├── [email protected]
└── [email protected]

Then, all we have left to do is run the app:

node app

You should get a confirmation message that your application is up and running (it defaults to port 3000):

$ node app
Express server listening on port 3000

Open up a browser and check that you have your app at localhost:3000. You should get something like in the following:

Express Default App Underwhelming but we’re just getting started.

Not the most exciting of beginning but we’ve created a full application in Node with just the commands:

npm install express
express --sessions --css less
npm install

In part 2, we’ll take a closer look at what we’ve created.