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.
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.
Write your short JS/Node program in a file in the same directory. I called mine “app.js”.
Start it up.
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.
Install the command-line utility by installing Express with the
-g flag. I also explicitly asked for Express 3.x (vs. Express 2.x).
Now you should be able the run the command line utility. Let’s check the version.
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.
Now we can have express(1) create the app shell for us.
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.
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.
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.
Open ./routes/index.js to see the definition.
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.
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.
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.
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).