Express 4 Is Out and the Node.js Agent Supports It!

We just released an update to our Node Agent which supports instrumentation of apps written in Express 4; you can read more about that here. I’ll walk you through some of those features and elaborate on how it affects you!

Express Logo

If you are a web developer using Node.js, you’ve most likely encountered Express at some point. Express is often the first framework people use in Node and puts the ‘E’ in the MEAN stack. Earlier this month, version 4 of the Express framework was released with some pretty cool features, especially for teams working on the same site. The biggest difference between Express versions 3 and 4 is an improved approach to routing. These changes enable teams to work on the same code base more effectively. The specific improvements in Express 4 range from small to large:

  • The ability to use `:variables` in middleware urls, to cover some additional use cases.
  • Defining a path, then attaching HTTP verbs to it, to remove redundancy.
  • Attaching modular Route objects under a specific path, to make concurrent collaboration easier.

Next, I’ll give you a more detailed look at two of the routing changes Express 4 introduced, and how this will make your code simpler and more collaborative.

Attaching Router Instances

Express 4’s approach to attaching `Router` instances allows you to make your code more modular. This means you can build packages that export a `Router` so they are more easily reused, or even able to be worked on by separate teams within your company. Once those modules packages are built, you can easily compose them into a single site, or break them apart into their own services as needed. To dive into how exactly this works, let’s take a look at a snippet of code to help explain what attaching a `Router` means. Say you have a set of views for your users to manage their accounts. We’ll put them in lib/account.js:

 // lib/account.js var express = require('express'); // create the router object var accounts = express.Router(); // middleware that requires the user to be authed accounts.use(function(req, res, next) { if (req.authed === true) { next(); } else { res.redirect('/login'); } }); // Profile view accounts.get('/profile', function(req, res) { // get the user's data and show it }) module.exports = accounts 

Then in your index.js you can do something like:

 // index.js var express = require('express'); var app = express() // Add middlewares and other higher priority views here // This is where we attach our views app.use('/account', require('lib/account')) 

Attaching HTTP Verbs to Paths

Another feature that will enable more clear and less repetitive code, is the new approach of adding HTTP verbs to paths. This approach clearly separates the GET, POST or other calls to a URL, while still allowing you to change the URL in a single place. Here is an example:

 app.route('/update') .get(function(req, res) { /* return the blank form */ }) .post(function(req, res) { /* update the record */ }); 


In summary, the part of Express that everyone directly interacts with the most — the adding of routes — has been updated and significantly improved. Even if you are already building Express apps, this is not a version to skip. It will let you refactor your code to be much more readable and maintainable, and everyone loves quality software.

Wraithan started programming in C++ to write his own video games, then later to cheat at them. He discovered Python and had a blast for a few years in the community. He picked up Node on a whim to build an IRC bot and fell in love. Wraithan enjoys listening to metal, biking, skateboarding, writing IRC bots, playing with hardware, and helping to run PDX Node. View posts by .

Interested in writing for New Relic Blog? Send us a pitch!