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 🙏

© 2024 – Pkg Stats / Ryan Hefner

commuter

v2.0.1

Published

A minimal, composable router that supports sub-routes.

Downloads

339

Readme

commuter

A minimal, composable router that supports sub-routes.

Build Status npm install js-standard-style

Example

var commuter = require('commuter')

var router = commuter()

router.get('/post/:title', onRoute)

//later, a GET request is made with the url '/post/some-title'
router(req, res)

function onRoute(req, res) {
  console.log(req.params.title) // 'some-title'

  // handle route...
}

Subroutes work exactly as you'd expect:

var commuter = require('commuter')

var router = commuter()
var subrouter = commuter()

router.get('/post/*', subrouter)
subrouter.get('^/?view/:title', onRoute) // optionally leading slash

// later, a GET request with url '/post/view/some-title'
router(req, res)

function onRoute(req, res) {
  console.log(req.params.title) // 'some-title' 
  console.log(req.url) // '/post/view/some-title'

  // handle route...
}

Even handle the "index" route in your subrouter:

var commuter = require('commuter')

var router = commuter()
var subrouter = commuter()

router.get('/post/*', subrouter)
subrouter.get('^$', onRoute) // optionally leading slash

// later, a GET request with url '/post/'
router(req, res)

function onRoute(req, res) {
  console.log(req.url) // '/post/'

  // handle route...
}

API

  • commuter([defaultRoute] [, root] [, verbs]) - Create a new router. Accepts the following parameters:
    • defaultRoute - A function to be called if no routes are matched
    • root - A string to be ignored at the begging of any URL; for example, passing /some/string will cause the route /some/string/with/more to be matched using only /with/more
    • verbs - By default, the standard HTTP verbs are supported: get, post, put, patch, delete. If you need different verbs, pass them here. It will replace the defaults, excepting the special any route, which is always available.

The router that is returned has the following methods:

  • router(request [, args ...]) - Route a request through the router. Routes are matched in the order they were added.
  • router.<method>(pattern, fn) - Define a route on your router
    • <method> - Any of the standard HTTP verbs, or the verbs you defined; for example, router.get, router.post, or router.any.
    • pattern - A string that is either a Cucumber-style pattern describing a URL or a Regex string (not a RegEx object). commuter uses routes for it's pattern matching, and follows those docs and rules.
    • fn - The function to be called when your route is matched. This function should take the same form as your router.<method> function; that is, if your router was called as router(req, res), your function will be called with fn(req, res)

The request object only needs to be "request-like"; that is, the only properties that are used are request.url to match the url, and optionally request.method, which will default to the router.any routes if missing.

As the request passes through the router, a few additional properties are added to it:

  • request.params - A key/value object of the matched parameters from your pattern, and their captures values.
  • request.splats - An array of the matched splats
  • request.route - The last route pattern that was matched.

There are a few other additions that come via routes and are explained in their docs.

License

MIT. See LICENSE for details.