npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

dispatchington

v0.0.7

Published

Trie-based URL and Method Routing for Node.js

Readme

Dispatchington Build Status

Dispatchington is a trie-based URL and method router for Node.js based on routington. It can almost be used as a drop-in replacement. For information on its motivations, read about The Imperfections of Express.

When should you use this?

  • Want your server to conform to HTTP specs
  • Want to decouple your app away from Connect/Express
  • Prefer a more modular routing framework
  • Prefer explicit routing
  • You repeat a lot of route definitions
  • You want structure to your URL routes - routes are stored in a branch tree

Features

  • Supports the OPTIONS method automatically.
  • Supports 405 Method Not Allowed automatically.
  • Supports 501 Not Implemented automatically, assuming you use the middleware.
  • Allows you to "define" middleware stacks for easy re-use.
  • Faster as it uses string matching instead of regular expression matching whenever possible.

Benchmark

See /benchmark for the benchmark test. The take away of this benchmark isn't that Dispatchington is faster at routing, but that its performance is more precise. In other words, it "scales" with the number and complexity of routes.

Note that these benchmarks are not comprehensive and that the differences in speeds are due to more just the route being matched.

API

Mounting to Connect/Express

var dispatchington = require('dispatchington')
var router = dispatchington()

To mount the middleware:

var app = express()
app.use(router.implementedMethods)
// static stuff
// cookie stuff
// session stuff
app.use(router.dispatcher)
// 404 handler
// error handler

Standalone

If you're not using Express or Connect, it will still work:

var dispatcher = router.dispatcher
var methods = router.methods

// ...
// Define routes
// ...

http.createServer().on('request', function (req, res) {
  if (!methods[req.method.toLowerCase()]) {
    res.statusCode = 501
    res.end('Not Implemented')
    return
  }

  dispatcher(function (req, res, next) {
    if (err) {
      res.statusCode = err.status || 500
      res.end(err.message || 'Internal Server Error')
      return
    }

    // If this point is reached,
    // no route has been matched
    res.statusCode = 404
    res.end('Not Found')
  })
}).listen(process.env.PORT || 80)

Defining middleware

router.define('compress', connect.compress())
router.define('session', [
  connect.cookieParser(),
  connect.cookieSession()
])

You will define middleware stacks which will then be referrable by name. For example:

router.get('/', 'compress', 'session')

is equivalent to all of the following:

router.get('/', connect.compress(), connect.cookieParser(), connect.cookieSession())
router.get('/', connect.compress(), [
  connect.cookieParser(),
  connect.cookieSession()
])
router.get('/', [
  connect.compress()
  connect.cookieParser(),
  connect.cookieSession()
])

The idea is that you turn your middleware stacks into legos. You can define them early in your app, then re-use them throughout your routes without require()ing each of them from an external "common" module. It will also make your routes much more readable as, ideally, it could look something like this:

router.get('/',
  'compress',
  'cache if visitor',
  'accept html only',
  'retrieve session',
  'retrieve user',
  'check if allowed',
  'parse body',
  handler
)

function handler(req, res) {
  res.render('homepage')
}

Defining routes

For information on how the routing strings actually work, view routington's documentation.

There are two ways to create a route. The first is Express' method-first way:

router.get('/things', 'compress', 'session', handler, ...)
router.post('/things', 'compress', 'session', handler, ...)
router.put('/things', 'compress', 'session', handler, ...)

The other is by defining a route, then adding stacks based on the method:

router.route('/things')
.get('compress', 'session', handler, ...)
.post('compress', 'session', handler, ...)
.put('compress', 'session', handler, ...)

In both versions, you can define multiple routes at the same time using an array:

router.route(
  '/things',
  '/thing/:id'
)
.get('compress', 'session', handler, ...)
.post('compress', 'session', handler, ...)
.put('compress', 'session', handler, ...)

Parameter matching

If you define a route like so:

router.get('/thing/:id', handler)

You can retrieve the value of id just like in Express:

function handler(req, res, next) {
  req.id = req.params.id
  next()
}

Accessing the trie

The trie, specifically the routington instance, is accessible at router.trie. You can manipulate it however you'd like. View its documentation for more information.

Unsupported Express features

  • Wildcard routes are not supported. The purpose of this router is to make it easier for you to be explicit.
  • app.all is not supported. Simply define a stack and use it in every method. npm install methods for a list of all possible methods.
  • Regular expressions (as a variable type) are not supported as input arguments. If you want to use regular expressions, just stick with Express' router.

License

WTFPL

© Jonathan Ong 2013

[email protected]

@jongleberry