Express is a minimal and flexible Node.js web application framework, providing a robust set of features for building single and multi-page, and hybrid web applications.
On 9th April 2014, new version 4.0 of Express web application framework has been released. There are some significant changes which you should be aware of before switching to the latest version.
Express generation
The express
command has been moved to the QuickStart project generator express-generator. You can find out more about the new module here.
Node 0.8.x support
Express 4.0 does not support Node 0.8.x anymore. That might be an issue if you use any modules which require Node 0.8.x.
Configuration
The conditionally invoked callback configuration method: app.configure()
has been removed. In version 4.0 if
statements should be used instead e.g.
Version 3.x
// all environments
app.configure(function() {
app.set('title', 'Application Title')
})
// development only
app.configure('development', function() {
app.set('mongodb_uri', 'mongo://localhost/dev')
})
// production only
app.configure('production', function() {
app.set('mongodb_uri', 'mongo://localhost/prod')
})
Version 4.0
// all environments
app.set('title', 'Application Title')
// development only
if ('development' == app.get('env')) {
app.set('mongodb_uri', 'mongo://localhost/dev')
}
// production only
if ('production' == app.get('env')) {
app.set('mongodb_uri', 'mongo://localhost/prod')
}
Removed Bundled Middleware
All bundled middleware except express.static()
has been moved into separate repository - expressjs. So as of 4.x, Express no longer depends on Connect module.
A list of middleware modules supported by the Connect/Express team:
Express 3.x Module | Module Replacement for Express 4.x |
---|---|
bodyParser | body-parser |
json | body-parser |
urlencoded | body-parser |
compress | compression |
timeout | connect-timeout |
cookieParser | cookie-parser |
cookieSession | cookie-session |
csrf | csurf |
error-handler | errorhandler |
session | express-session |
method-override | method-override |
logger | morgan |
response-time | response-time |
directory | serve-index |
static | serve-static |
favicon | static-favicon |
vhost | vhost |
Note that some middleware modules previously included with Connect are no longer supported by the Connect/Express team. You can refer to http-framework GitHub project for other compatible middleware modules.
Checkout here for more module alternatives.
Routing changes
Approach to routing has been improved. New changes give the developer more control and a more robust solution for building bespoke routes.
New app.route()
has been added as a shortcut to the Router
. Using app.route()
is a recommended approach to avoiding duplicate route naming and thus typo errors e.g.
var app = express();
app.route('/login')
.all(function(req, res, next) {
// Perform some operations common to login page rendering and loggin operation
})
.get(function(req, res, next) {
res.render(...); // render login page
})
.post(function(req, res, next) {
// Perform login operation i.e. user name & password check etc.
})
Router API has been added. A router is an isolated instance of middleware and routes. We are able to add specific middleware functions to particular routes e.g.
/api
can use IP restrictions middleware./admin
can use authentication check middleware.- both can use logging and processing time middleware.
Some features of Router
:
- Use
express.Router()
multiple times to define independent groups of routes. - Apply the
express.Router()
to a section of your site usingapp.use()
e.g.app.use('/api', apiRouter)
. - Use route middleware to process requests.
- Use route
.param()
middleware to validate parameters or preform preloading operations.
You can read more on Routing in Express 4.0 on Router documentation.
Application local variables
The app.locals
is no longer a function. It is a JavaScript object now. As of 4.x locals merging will not happen automatically as in version 3.x
Version 3.x
That operation merges key1
and key2
key-value pairs to app.locals
.
app.locals({
key1: value1,
key2: value2
})
Version 4.0
app.locals.key1 = value1
app.locals.key2 = value2
Minor changes and improvements
A set of minor changes and improvements has been added like: req.params
is now an object instead of an array. There were some issues with req.params
being an array before, which you can find on this Stack Overflow question.