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

rout

v1.0.1

Published

Middleware style router with configurable pattern matching/parsing, vhost and namespacing.

Downloads

15

Readme

Rout

Middleware style router with configurable pattern matching/parsing, vhost and namespacing.

Gaffgarion

Install

npm install rout

Y!?

Why did I add to npm's 2526 packages that match the search "router"?

  • Mostly because every other router I know of has a built-in parser. That bothers me.
  • A lot of routers don't make their state (routes) available to the outside world. Does it matter? Better safe than sorry. Internal state is bad enough anyway.

Usage

// setup the router with a parser
var UrlPattern = require('url-pattern');
var parser = function(pattern, url) {
  return new UrlPattern(pattern).match(url);
}
var Router = require('rout').bind(null, parser);

// create a router instance
var router = new Router();

// add a route (there are other ways to do this)
router.get('/foo', function(req, res) {
  res.end('foo');
});

// use as middleware
http.createServer(router);

Router(parser, routes, patternModifier)

parser(pattern, url)

Function that matches url to pattern and parses parameters from url.

  • if the url matches the pattern:
    • if there are parameters, return an object of parameters
    • if there are no parameters, return an empty object
  • if the url does not match the pattern:
    • return false

url-pattern is a great parser for routers.

routes

Pass in an array of routes to set initial state.

patternModifier(pattern)

This function modifies the pattern that will be passed to the parser. The return value is the pattern that will be used by the parser.

Manual route creation

A route can be a handler function or an object with the properties method, pattern and handler.

var routes = [
  // handler for all requests
  function(req, res, next) {
    console.log(req.url);
    next();
  },
  // basic route
  {
    method: 'get',
    pattern: '/foo/:id',
    handler: function(req, res, next, params) { res.end('foo: '+params.id); }
  },
];

method

The http method of the request. Not case-sensitive.

If not present or falsy, the route will match all methods.

pattern

The pattern to test the request url against.

If not present or falsy, the route will match all urls.

handler(req, res, next, params, data)

  • req - request object
  • res - response object
  • next - callback to continue to the next matching route, optionally passing data to that route handler
  • params - url parameters
  • data - a data object that persists across handlers for a request

Helpers / route generation

router[method](pattern, handler), router[method](handler)

Methods: get, post, put, patch, delete, options, all

// add route that handles all methods and all urls (all requests)
router.all(function(req, res, next) {
  next();
});
// add route that handles all get requests
router.get(function(req, res) {
  res.end();
});
// add route that handles post requets to /foo
router.post('/foo', function(req, res) {
  res.end();
});

router.routes

Routes are stored on router.routes so that they can be easily accessed and manipulated.

Middleware

router.all(favicon(__dirname+'/public/favicon.ico'));

router.vhost(pattern, handler)

Uses the configured parser to match the host to the pattern.

If the request host matches the pattern, the handler will be called.

// direct anything with a "foo" subdomain to fooRouter
router.all(router.vhost('(http(s)\\://)foo.:domain(.:tld)(\\::port)', fooRouter));

router.namespace(pattern, router)

Returns a router with the same parser and state as router, but all route patterns are parsed with pattern prefixed.

var router = new Router();

var subRouter = new Router();
subRouter.get('/bar', function() {});

// /foo/bar will be handled by the subRouter's /bar
router.all(router.namespace('/foo', subRouter));