Express 4.0: Router improvements, Removes Bundled Middleware; But No Major Plans for Future
An addition to version 4.0 is the public API for express Routers. A Router is like a mini express app, it contains no views or settings but does provide the typical routing APIs (.use, .get, .param, .route).
In the release notes on GitHub TJ Holowaychuk, creator of Express, said the new version “exposes a better Router and route API to curb many instances of duplicating route naming”. Holowaychuk said the release also makes it easier to split Routers into files for project organization.
InfoQ contacted contributor Roman Shtylman who said: “We hope the router will expose more users to the idea of modularized routes and apps. By exposing the Router in Express we emphasize the ease with which developers can separate routing logic into files and create re-usable routes without having to instantiate new apps.”
These routers inherit the settings and view engine of the app they are used under to versus supplying their own. The hope within the Express team is that this will lead to more maintainable applications.
Also added is a new route API to address the issue of duplicating the same route string for multiple Http verbs. Shtylman says the idea was to help avoid typos.
Another change with 4.0 is the introduction of bundled middleware. As of the new version, the framework no longer depends on Connect. All of the previously included middleware are now in separate repos.
Previously, fixes to a small issue in any of the roughly two dozen middleware for Express meant that a new version of Express would need to be released each time. As a result, upgrades were more time consuming, and significant changes were buried among minor upgrades.
“By separating out all of the middleware into modules, we can add more maintainers as well as release updates only for the affected modules. This leads to easier to grok upgrades.” Stylman said.
A complete list of middleware is available, the only included middleware is now express.static().
Commenting on the release announcement on Hacker News, user jacqued said “The changes make Express even cleaner than before. The new routing system, allowing handling different verbs with chained functions, is pretty slick.
“Also, decoupling default middleware and putting them into their own modules seems like the way to go, and more 'node-y' than packing everything together.”
Irakli Janiashvili, developer of jani.io, agreed: “I really like this new [router] feature. This will help me build better architecture for applications on top of Express,” he said.
Other changes include eliminating the most common issue in Express by replacing app.use(app.router) with routing methods being added in the order in which they appear.
In addition, instead of using the app.get(), app.post(), verbs directly (which are still supported), a new route() method, which returns an instance of a Route, can be used. Users can then call all of the HTTP verbs on this instance, and even add middleware specific to this route.
When asked about the Express roadmap, Shtylman said the team had “currently no major plans” for future releases, other than supporting existing Express users, and fixing bugs.
“We don’t want users to get too hung up about "major" version numbers. If anything, we might iterate changes faster if they lead to a cleaner more maintainable Express core. Our goals are to avoid "break the world" types of releases that some projects do every few months or years. We would rather release more modules faster and allow users to selectively upgrade the pieces they need.” Said Shtylman.