Express 3.x to 4.0 migration

Express is a minimalist web development framework and one of the most popular modules for Node.js. The new 4.0 version of Express brings a better router and drops bundled middleware, so we decided to migrate from verstion 3.x to the newest version.

Changes

Express 4 does not depend on middleware framework connect anymore, which means that middlewares like bodyParser, session, favicon are not longer explicitly available. Instead, creators of the framework moved the middlewares to separate modules to improve their release cycle without affecting main express release. Most of these middlewares are ports from connect 2.x equivalents.

You have to add these new modules to package.json and require them in the code explicitly.

New stuff

Express brings a better router to make it easier to split routers into files/modules for project organization without sacrificing features like parameter matching.

In addition, app.route() method provides an instance of route, which can be used to call HTTP verbs on this or add middleware. A Router also could play a role of mini express application, providing typical routing APIs.

  app.route('/users')
  .get(function(req, res, next) {})
  .post(function(req, res, next) {}) 

Instead of causing a lot of issues app.router, now middleware and routes are executed in the order they are added in the source code.

To create a new app, use express(), because long deprecated express.createServer() does not exist anymore.

If you need to configure different routes based on environment, use if statement instead of app.configure().

  var env = process.env.NODE_ENV || 'development';
  if ('development' == env) {
      // configure stuff here
  }

References:


comments powered by Disqus