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.
- Part 1: Introduction
- Part 2: Installation and Skeleton App, source code commit: 4ea936b
- Part 3: RESTful Methods and Testing, source code commit: 39e66cb
- Part 4: Templates, Partials, Creating and Editing Documents, source code commit: f66fdb
- Part 5: Authentication, Sessions, Access Control Middleware, source code commit: 03fe9b
- Part 6: Interface Basics, source code commit: f2261c
- Part 7: Node Library Versions, Jade Tricks, Error Pages, source code commit: 929f5
- Part 8: Flash Messages and Helpers, source code commit: 841a49
- Part 9: Remember Me, source code commit: 1904c
- Part 10: Markdown, source code commit: 11d33
- Part 11: Better Testing, source code commit: 6a269ce
- Part 12: Updating Mongoose, source code commit: 2a8725
- Part 13: Stylus, source code commit: 0a70e40
- Part 14: Email, source code commit: 2e81a7b
- Part 15: Node Versions, npm Publishing, source code commit: 89abea1
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.
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 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 email@example.com. 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:
http.js:521 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 19fbb2.