2011-03-14 00:00:00 +0000 by Alex R. Young
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
Click to show previous tutorials.
\* [Part 1: Introduction](http://dailyjs.com/2010/11/01/node-tutorial/)
\* [Part 2: Installation and Skeleton
App](http://dailyjs.com/2010/11/08/node-tutorial-2/), source code
\* [Part 3: RESTful Methods and
Testing](http://dailyjs.com/2010/11/15/node-tutorial-3/), source code
\* [Part 4: Templates, Partials, Creating and Editing
Documents](http://dailyjs.com/2010/11/22/node-tutorial-4/), source code
\* [Part 5: Authentication, Sessions, Access Control
Middleware](http://dailyjs.com/2010/12/06/node-tutorial-5/), source code
\* [Part 6: Interface
Basics](http://dailyjs.com/2010/12/20/node-tutorial-6/), source code
\* [Part 7: Node Library Versions, Jade Tricks, Error
Pages](http://dailyjs.com/2010/12/27/node-tutorial-7/), source code
\* [Part 8: Flash Messages and
Helpers](http://dailyjs.com/2011/01/03/node-tutorial-8/), source code
\* [Part 9: Remember
Me](http://dailyjs.com/2011/01/10/node-tutorial-9/), source code commit:
\* [Part 10: Markdown](http://dailyjs.com/2011/01/24/node-tutorial-10/),
source code commit:
\* [Part 11: Better
Testing](http://dailyjs.com/2011/01/31/node-tutorial-11/), source code
\* [Part 12: Updating
Mongoose](http://dailyjs.com/2011/02/07/node-tutorial-12/), source code
\* [Part 13: Stylus](http://dailyjs.com/2011/02/14/node-tutorial-13/),
source code commit:
\* [Part 14: Email](http://dailyjs.com/2011/02/21/node-tutorial-14/),
source code commit:
\* [Part 15: Node Versions, npm
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
npm install n
Then switch to 0.4.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
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 . 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
bodyDecoder are now
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
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
expressjs.com/guide.html 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