Migrate to Express 4

John Lindquist
InstructorJohn Lindquist
Share this video with your friends

Social Share Links

Send Tweet
Published 10 years ago
Updated 5 years ago

Express 4 is almost ready for release. The previous lessons looked at Express 3, but will use Express 4 going forward. This lesson will explain some of the main breaking changes.

Man: [00:01] Express 4 is starting to reach the release candidate phases. Let's migrate to Express 4 now so that we can focus on that moving forward.

[00:07] I'll "npm express install express@4.00RC3" to grab the third release candidate, the latest as of today. When I try and run this, we'll get some good errors telling us what's going on.

[00:19] Middleware is no longer bundled. What that means is that this, where the JSON body parser stuff was bundled before, is no longer bundled. That means we will need to "npm install body-parser." We will need to require body-parser, and then, where we used this middleware before, we'll replace that with body-parser and invoke it.

[00:51] Now, if we run this, everything is up and running. Looks like everything is working fine.

[00:58] The other major feature to talk about that we've covered so far is that instead of using get and post on this people route and defining it twice, I can instead say app.route and then call get and post on that, because app.route now returns a router.

[01:17] We'll format that and delete this, delete the string. This now, if I rerun, will work just fine as well. This wouldn't have worked before. It's a new feature we're calling app.route. Passing it a string will let you chain these together -- get, post, and whatnot.

[01:38] There are some other things that have changed. App.router is removed and app.configure is removed. But we never got to that stuff, so I'm not going to worry about it here. We're simply going to focus on what Express 4 does provide moving forward.

[01:50] Lastly, there is a migration guide on Express's GitHub page. Head over to this link if you're curious about what other minor things have changed for this release.

Julian
Julian
~ 9 years ago

If you are getting a "body-parser deprecated" message.

Replace: app.use(bodyParser());

With: app.use(bodyParser.urlencoded({extended: true}));

Markdown supported.
Become a member to join the discussionEnroll Today