Getting Started With Node.js and Express 3

I suspect quite a few of those starting with Node are interested in using it for web development. Of the web frameworks available for Node, Express is leading the pack. And version 3 is in beta. I’m just getting started with Node and Express, so I thought I’d help you all get up and running that much more quickly. I won’t get into the boring details of installing Node and NPM. That part is especially easy now that they have Windows and Mac installers available on the Node website.

The Minimalist Approach

If you wanted to keep things small and simple, you could create an entire Express application in a single file. Just navigate to your target directory where you want the app to reside, and install Express locally.

1
$ npm install [email protected]

Write your short JS/Node program in a file in the same directory. I called mine “app.js”.

1
2
3
4
5
6
7
8
9
10
11
12
// Include the Express library, and create your "app" (an HTTP server).
var express = require('express'),
app = express.createServer();

// Define a handler for your root URL, which sends a text response.
// By default the Content-Type header will be "text/html".
app.get('/', function(req, res) {
res.send('Bonjour tout le monde!');
});

// Have the server start listening on port 3000.
app.listen(3000);

Start it up.

1
$ node app.js

Now navigate to http://locahost:3000/ to see your handiwork.

Although that’s one way to get started, I suggest starting with the express(1) command-line utility, which generates the boilerplate for you. Using it, you’ll get a nice little skeleton for your web app that you can extend.

Installing the Command-Line Utility

Install the command-line utility by installing Express with the -g flag. I also explicitly asked for Express 3.x (vs. Express 2.x).

1
$ npm install -g [email protected]

Now you should be able the run the command line utility. Let’s check the version.

1
2
$ express --version
3.0.0beta4

It’s worth mentioning that when I upgraded from Express 2, I got the error “Error: Cannot find module ‘commander’” when running express(1). I could get past this by installing it a second time, or by uninstalling the old version before installing the new one.

Creating a New App

Now we can have express(1) create the app shell for us.

1
$ express --sessions mywebapp

That’ll create the directory “mywebapp”, the entry-point for your program named “app.js”, and a bunch of other files. I also included the --sessions option to enable sessions, because that’s a common feature many people will likely use.

One thing it doesn’t do is to install the Node module dependencies. Use NPM to do that for you.

1
$ npm install

Dependencies are listed in the package.json file, so this is enough to install them all. Express itself is a dependency, and so is Jade, the default template engine.

Now you can start your web app, which runs at http://locahost:3000/ by default.

1
$ node app.js

A Short Walkthrough

Let’s step through the basic handling of a request.

In app.js, we see the handler for the root URL. This handler supports the GET HTTP method. Similar methods on the app object are used to attach handlers to other HTTP methods (POST, DELETE, etc). We also see that the handler, itself, is defined in the ./routes module.

1
2
3
var routes = require('./routes');
...
app.get('/', routes.index);

Open ./routes/index.js to see the definition.

1
2
3
exports.index = function(req, res){
res.render('index', { title: 'Express' });
};

A request handler is passed two arguments—the request and response objects. In the minimalist approach, we already saw that res.send() will simply send whatever string you give it as the response. Here, we see res.render(), which is used for generating a response from a template engine. The first argument is the name of the template. The second is an object with all the values you want your template to have access to.

The template engine and default directory for your templates is defined in app.js.

1
2
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');

The named template was “index”. So you’ll find that at views/index.jade. With Express, you can leave off the extension if it has the same name as your default template engine.

You can make changes to template while your app server is running—at least in development mode—and you’ll see them as soon as you refresh the page. With your server running, try adding a line, like I did here, and refresh the webpage.

1
2
3
4
5
6
extends layout

block content
h1= title
p Welcome to #{title}
h2 All your Node are belong to us!

However, changes to your JS files require you to restart the server to take effect.

So to add a new page, you’d create a template file in the views folder, create a new request handler in routes/index.js, and then attach the request handler to the app object in app.js.

Closing Thoughts

I’ve taken a liking to web micro-frameworks like this. What I really want from a web framework is request routing, templating, and session support. So for me, Express delivers.

Sadly, the documentation is lacking. The 2.x guide doesn’t document the full API. The 3.x documentation hasn’t been released yet, but I’m hoping it’ll have more coverage. Luckily, the code base is small—about 1000 source lines of code—so you can always dig into the source when necessary (or curious).

Here are a few links to some helpful documentation: Express, Jade, Node, and NPM. Happy building!

Author: Guest

Author: Guest Sometimes someone asks if they can write for the blog. They may want to just work on their own writing chops, get their foot in the blogging door, or maybe they want to show off something they've done. In any case, they are a guest author and this post happens to be written by one; please enjoy the hard work they've done to put this article together.