Express 3.0.0a is here

Apr 26, 2012

The Node.js module called express has now released its 3.0.0a version. For me, express is one of the packages that I always use in my node projects. It's so easy to get up and running with it and there are tons of examples and tutorials (many outdated unfortunately) from where you can get answers and inspiration.

With the new version, I think many applications will break though because some of the more basic syntaxes has been changed. For example, the way you create the server object has been updated. Earlier you would probably use a syntax like this:

var express = require('express'),
    app = module.exports = express.createServer();

And in version 3 you would instead do it like this:

var express = require('express'),
    app = module.exports = express();

At first when you look at this, it doesn't seem like a very big change at all. You would think that the only change is the lack of using the function createServer() but it is a little more different than that. Because when we used to call the createServer() it returned a http.Server object and now that we only use the syntax express() we get a express.application object instead. The difference might not be that big at first but when you start mixing it up with different packages, things will probably start crashing on you.

One example of a package that doesn't work like it used to because of that is the also popular socket.io. Here is an example of how it could look like in combination with express 2.x:

var express = require('express'),
    app = module.exports = express.createServer(),
    io = require('socket.io').listen(app);

But since the app object is not a http.Server anymore, this way will not work in 3.x. Instead, you have to write it something like this:

var express = require('express'),
    app = module.exports = express(),
    server = app.listen(3000),
    socket = require('socket.io').listen(server);

The difference here is that once you call app.listen(3000) you get the old http.Server object like we used to. And then you can pass that to your socket object to get the same result as before.