Node Tutorial Part 16: Updating to Node 0.4.2 and Express 2

14 Mar 2011 | By Alex Young | Tags server node tutorials lmawa nodepad npm

Welcome to part 16 of Let’s Make a Web App, a tutorial series about building a web app with Node. This series will walk you through the major areas you’ll need to face when building your own applications. These tutorials are tagged with lmawa.

Click to show previous tutorials.

Switching to Node 0.4.2

I can tell you’re all desperate to get Nodepad on Node 0.4.x, so let’s do it! Node and even libraries like Express are still volatile, so expect things to break. If you’re running production Node apps, you’ll need to factor in some time to update Node and your dependencies as part of your project management. It’s just a fact of life at the moment.

Back in tutorial 15 I demonstrated how to switch between Node versions using n. Make sure you’ve got n installed:

npm install n

Then switch to 0.4.2:

n 0.4.2

Express 2

I haven’t seen TJ say too much about Express 2 yet, but I’ve noticed he’s added some interesting new projects to his visionmedia GitHub account (express-resource and express-namespace look interesting).

So it seems like although Express 2 is fairly similar to Express 1, the project will get new supporting modules that you can use if you want. It’s almost like an anti-Rails or Django. So let’s take a look at the API changes that affect our app.

To install the Express 2 I’ve got, just type npm install express@2.0.0beta3. I’ve updated app.js and package.json with the other library versions I’m using.

Express 2 API Changes

Some of the middleware has been renamed. So cookieDecoder and bodyDecoder are now cookieParser and bodyParser. The staticProvider middleware has been changed to static. I’ve made these changes in our app.

The view partial rendering is also slightly different. Before a views/partials directory was the default location for partials, but now it’s relative to the current template’s directory. That means using partial('fields') in views/users/new.jade will look for views/users/fields.jade. This seems more consistent to me so I didn’t mind the change.

From the documentation:

View lookup is performed relative to the parent view, for example if we had a page view named views/user/list.jade, and within that view we did partial(‘edit’) it would attempt to load views/user/edit.jade, whereas partial(‘../messages’) would load views/messages.jade.

Header modification is also now stricter. I made a mistake where an asynchronous Mongoose call could happen after a redirect was sent. In such cases an exception will now be raised:

    throw new Error("Can't use mutable header APIs after sent.");

If you see something like that in your own apps, try looking at the order of things are happening given that you’re likely to be using a lot of asynchronous calls.

And it looks like refers to the latest version, so have a read of that to get up to speed.


Upgrading major versions of your platform and dependencies is probably almost always difficult. TJ has written a migration guide for Express 1.x to 2.x developers, so you should be OK migrating your own projects. Just remember it’s likely that many dependencies will need to be updated at once: obviously upgrading Express implies a Connect update.

The latest commit is 19fbb2.

blog comments powered by Disqus