Skip to content

v2.3.0

Latest

Choose a tag to compare

@UlisesGascon UlisesGascon released this 17 Nov 08:26
d4f4d86

Highlights

router now has built-in tracing 🎉

Until now, observability tools had to monkey-patch the router to see what happens inside a request. That breaks easily and every tool did it in its own way.

router now publishes to the Node.js diagnostics_channel through a TracingChannel named express.router.request. Every middleware, route handler, error handler and router.param() callback is traced, and the context includes the matched route pattern (/users/:id, not /users/42). With no subscribers, the tracing work is skipped.

const dc = require('node:diagnostics_channel')

function report (ctx) {
  const ms = (performance.now() - ctx.startTime).toFixed(1)
  const name = ctx.handler.name || '<anonymous>'
  console.log(`${ctx.req.method} ${ctx.route} ${name} ${ms}ms`, ctx.error ? 'failed' : 'ok')
}

dc.tracingChannel('express.router.request').subscribe({
  start (ctx) { ctx.startTime = performance.now() },
  // `result` or `error` at `end` means the handler was synchronous
  end (ctx) { if ('result' in ctx || 'error' in ctx) report(ctx) },
  asyncEnd (ctx) { report(ctx) }
})

// GET /users/:id getUser 23.1ms ok
// DELETE /users/:id removeUser 0.1ms failed

Express 5 uses this router, so Express apps get the same channel once they update. The full context and the event rules are in the Diagnostics section of the README.

Thanks to @logaretm for driving this work (#196, #207, #223) and to @bjohansebas for the param callbacks and error attribution (#215).

List the registered routes with router.listRoutes()

A long requested feature: you can now ask a router what it has registered, without reading its internal stack.

const router = Router()
const admin = Router()

admin.get('/stats', (req, res) => res.end('ok'))
router.use('/admin', admin)
router.route('/users/:id').get(getUser).delete(removeUser)

router.listRoutes()
// [
//   { path: '/admin', methods: undefined, router: admin },
//   { path: '/users/:id', methods: ['GET', 'DELETE', 'HEAD'], router: undefined }
// ]

admin.listRoutes()
// [ { path: '/stats', methods: ['GET', 'HEAD'], router: undefined } ]

Mounted routers are not resolved for you, call listRoutes() on the router property to go deeper. Thanks to @bjohansebas (#174).

TypeScript declarations are included

Router and Route now ship with their own types, and you can describe what your middleware adds by augmenting Request and Response in the router module. Thanks to @gameroman (#197).

Also in this release

  • Malformed route parameters now set err.statusCode to 400 (#171)
  • req.url keeps its path separator when a mounted layer rewrites it (#206)
  • Route parameters without % are no longer decoded, which is faster (#188)
  • Layer match results are now local to each request (#216). If you rely on the internal Layer, note that it no longer stores keys, params or path, and layer.match() returns an object or false

What's Changed

New Contributors

Full Changelog: v2.2.0...v2.3.0