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

uroute

v1.1.0

Published

Microscopically small universal router

Readme

uroute 1.1.0

Microscopically small universal router

npm license travis greenkeeper mind BLOWN

.

uroute

.

Installation

npm install --save uroute

Require

var route = require('uroute')
var match = route.match
// or
var route = require('uroute').route
var match = require('uroute').match

Import

import route, { match } from 'uroute'
// or
import { route, match } from 'uroute'

Why

Basically a port of Universal Router (https://www.kriasoft.com/universal-router/) by Konstantin Tarkus from Kriasoft LLC, this module does not use generators or async functions, two upcoming features in Javascript. Cool as they are, these currently require the Babel runtime, along with the regenerator runtime/transform to be loaded. This added a lot of weight to my app which does not need these, so I refactored Universal Router to not depend on these features. The result is this module. It passes all original tests, though I had to make some changes (as the tests were also using async functions).

Usage

Define a route tree. This can be done with the route function:

var routes = [
  route('/', function(){
    return 'Welcome to the homepage of Cool Corp!'
  },

  route('/products', function(){
    return 'View our cool products'
  }),

  route('/about', function(){
    return 'About Cool Corp'
  })
]

This will create a route tree that looks like this:

[
	{
		path: '/',
		action: [Function]
	},
	{
		path: '/products',
		action: [Function]
	},
	{
		path: 'about',
		action: [Function]
	}
]

The use of the route function is purely optional syntax sugar for your convenience. If you want, you can create the route tree 'by hand' so to speak.

Once you have created a route tree, use match to find the matching nodes and invoke any actions on them:

var matched = match(routes, '/products')

match returns a Promise. Invoke it's then method to get access to the response:

matched.then(function(response){
	console.info(response)  //  View our cool products
})

match runs through all the routes in the tree, executing the actions on those routes that match the given url, until it finds one that resolves to a response that is not undefined. The first such response found is used.

Parameters

We can use parameters in our routes and they will be captured and made available on the params object of the context that is passed to our actions:

var routes = route('/:one/:two', function(context){
	return context.params.one + ', ' + context.params.two + ', ...';
})

match(routes, '/1/2').then(function(result){
	console.log(result) // 1, 2, ...
})
match(routes, '/un/deux').then(function(result){
	console.log(result) // 'un, deux, ...'
})
match(routes, '/eins/zwei').then(function(result){
	console.log(result) // 'eins, zwei, ...'
})

For more details refer to the documentation of path-to-regexp.

Async

Route actions may return promises.

Middleware

Route actions may act as middleware by utilizing the next and end functions passed to them on the context parameter. Let's create a simple middleware function to modify the repsonse of any of it's child routes:

function greeterMiddleware(context) {
	// use context.next() to get the next response in the chain
	return context.next().then(function(response){
		// augment the response in some way
		response = 'Hello' + (response ? ', ' + response : '')
		// return the modified response
		return response
	})
}

Now we'll set up our routes to have the middleware on the parent, like this:

var routes =
route('/hello',
	greeterMiddleware,
	[
		route('/world', function(){
			return 'world!'
		})
		,
		route('/planet', function(){
			return 'planet!'
		})
	]
)

The greeterMiddleware will modify the responses of it's children:

match(routes, '/hello').then(function(response){
	console.log(response)  // Hello
})

match(routes, '/hello/world').then(function(response){
	console.log(response)  // Hello, world!
})

match(routes, '/hello/planet').then(function(response){
	console.log(response)  // Hello, planet!
})

Issues

Add an issue in this project's issue tracker to let me know of any problems you find, or questions you may have.

Copyright

Copyright 2016 by Stijn de Witt. Some rights reserved.

License

Creative Commons Attribution 4.0 (CC-BY-4.0)

Based on Universal Router (https://www.kriasoft.com/universal-router/) Copyright © 2015-2016 Konstantin Tarkus, Kriasoft LLC. All rights reserved. License: MIT. SEE LICENSE-universal-router.txt.