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

boulevard

v2.3.5

Published

Handler-agnostic url routing

Downloads

27

Readme

<a href="http://badge.fury.io/js/boulevard">
	<img src="https://badge.fury.io/js/boulevard.svg" alt="npm version">
</a>
<a href="https://travis-ci.org/quarterto/Boulevard">
	<img src="https://travis-ci.org/quarterto/Boulevard.svg" alt="Build status">
</a>

Handler-agnostic URL router.

npm install boulevard

Usage

Takes a map of URLs (maybe with parameters) to functions and returns a function:

var route = require('boulevard');

route({
	'/': homeHandler,
	'/foo/:bar': barHandler
})

Handler arguments

Arguments are passed down from the returned function to the handler functions. This way, any shape of handler function can be used; the only requirement is that the request (or an object containing url) is the first argument. For example, you can use it directly with http.createServer:

http.createServer(route({
	'/': function(req, res) {
		res.end('home!');
	}
}));

Parameters

Parameters are, by default, passed as the last argument to the handler:

route({
	'/foo/:bar': function(req, res, params) {
		console.log('foo: ' + params.bar);
	}
});

Multiple matches

If there are multiple matching handlers, concrete matches are called before parameterised routes, and the first handler that doesn't return false wins:

route({
	'/foo/:bar': function(req, res, params) {
		console.log('foo: ' + params.bar);
	},
	'/foo/baz': function(req, res) {
		console.log('foobaz');
	}
});

Return values

Values returned from route handlers control what Boulevard does next. If you return false, control passes to the next matched handler (if any) or the 404 handler. If you return a non-false value or undefined, it considers your handling finished, and doesn't call any more handlers. The value is returned by the router.

Routes with the same path

If you need separate handlers at the same path (for example, a GET handler and a POST handler), you can pass in an array of maps or pairs instead of the the route map:

route([
	['/', function(req) {
		if(req.method === 'GET') {
			// ...
		} else return false;
	}],
	['/', function(req) {
		if(req.method === 'POST') {
			// ...
		} else return false;
	}],
	{'/': function(req) {
		if(req.method === 'PUT') {
			// ...
		} else return false;
	}}
])

404

If no routes match, a 404 handler is called. The default handler works with http.createServer; it sets the statusCode to 404 and sends an empty response.

Combining routers

concat

Returns a new router combining the routes.

var a = route({
	'/foo': function() {
		return 'foo';
	}
});

var b = route({
	'/bar': function() {
		return 'bar';
	}
});

var c = a.concat(b);
c({url: '/bar'}) //⇒ 'bar'
a({url: '/bar'}) //⇒ 404

use

Return a new router with the routes of the given router available as a subpath.

var a = route({
	'/foo': function() {
		return 'foo';
	}
});

var b = route({
	'/bar': function() {
		return 'bar';
	}
});

var c = a.use('quux', b);
c({url: '/quux/bar'}) //⇒ 'bar'
c({url: '/bar'}) //⇒ 404

Adding routes post-facto

The router function returned by Boulevard has an add method, which merges in a new map of routes:

var routes = route({
	'/': function() {
		return 'foo';
	}
});

routes({url: '/'}) //⇒ 'foo'
routes({url: '/bar'}) //⇒ 404

routes.add({
	'/bar': function() {
		return 'baz';
	}
});

routes({url: '/'}) //⇒ 'foo'
routes({url: '/bar'}) //⇒ 'baz'

Overriding default behaviour

As well as the default router, boulevard exports functions that allow you to override various parts of default behaviour. They each take functions to override and return a version of the router with the new behaviour.

Custom 404 handler (withFourOhFour)

Alias with404

Takes a handler of the same shape as your regular route handlers, which is called when none of the URLs match:

var myRoute = route.withFourOhFour(function(req, res) {
	res.statusCode = 404;
	res.end('Not found: ' + req.url);
});

Custom parameter munging (withAddParams)

Alias withParamHandler

Takes a function with arguments params (the parameters extracted from the URL) and args (the arguments passed to the original handler). Should return an array of arguments to pass to the matched handler.

var myRoute = route.withAddParams(function(params, args) {
	args[0].params = params;
	return args;
});

Custom URL gleaning (withGetUrl)

Gets passed the arguments from the router, should return the URL to match against. By default, parses the url to strip off any query string.

var myRoute = route.withGetUrl(function(req) {
	return req.url;
});

route_

route_ takes a hash of the above customisable functions as its first argument. Any not passed in is set to the default.

Licence

MIT.