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

@goatsystem/dispatch

v1.0.3

Published

A regular expression URL dispatcher for Connect

Downloads

5

Readme

Dispatch

A really simple URL dispatcher for Connect or a plain Node.js HTTP Server. Allows arbitrarily nested regular expressions for matching URLs and calling an associated function.

var Connect = require('connect'),
    dispatch = require('dispatch');

Connect.createServer(
    dispatch({
        '/about': function(req, res, next, queryParams){
            ...
        },
        '/user/:id': function(req, res, next, id, queryParams){
            ...
        },
        '/user/posts': function(req, res, next, queryParams){
            ...
        },
        '/user/posts/(\\w+)': function(req, res, next, post, queryParams){
            ...
        }
    })
);

Or, using a vanilla HTTP Server:

var http = require('http');

var server = http.createServer(
    dispatch({
        '/about': function(req, res, queryParams){
            ...
        },
        '/user/:id': function(req, res, id, queryParams){
            ...
        }
    })
);

server.listen(8080);

Dispatch can be used with a straight-forward object literal containing view functions keyed by URL. As you can see from the last URL in the list, captured groups are passed to the matching function as an argument.

You can also use :named parameters in a URL, which is just a more readable way of capturing ([^/]+). Named parameters are passed to the matched function in the same way as normal regular expression groups.

So far so predictable. However, it is also possible to nest these objects as you see fit:

Connect.createServer(
    dispatch({
        '/about': function(req, res, next, queryParams){ ...  },
        '/user': {
            '/': function(req, res, next, queryParams){ ...  },
            '/posts': function(req, res, next, queryParams){ ...  },
            '/posts/(\\w+)': function(req, res, next, post, queryParams){ ...  }
        }
    })
);

This helps you tidy up the structure to make it more readable. It also makes renaming higher-level parts of the path much simpler. If we wanted to change 'user' to 'member', we'd now only have to do that once. Another advantage of being able to nest groups of URLs is mounting reusable apps in your site tree. Let's assume that 'user' is actually provided by another module:

Connect.createServer(
    dispatch({
        '/about': function(req, res, next, queryParams){ ... },
        '/user': require('./user').urls
    })
);

Easy! A really lightweight and flexible URL dispatcher that just does the obvious.

Its also possible to define methods for URLs:

Connect.createServer(
    dispatch({
        '/user': {
            'GET /item': function(req, res, next, queryParams){ ... },
            'POST /item': function(req, res, next, queryParams){ ... },
        }
    })
);

Just prefix the URL with the http method in uppercase followed by whitespace and then the path you want to match against. Nested URLs always match the last method defined in the tree. Because of this, you can use the following style for matching request methods, if you prefer:

dispatch({
    '/test': {
        GET: function (req, res, next, queryParams) {
            ...
        },
        POST: function (req, res, next, queryParams) {
            ...
        }
    }
})

A couple of implementation points:

  1. The regular expressions automatically have '^' and '$' added to the pattern at the start and end or the URL.
  2. Only the first match is called, subsequent matches for a URL will not be called.
  3. If there are no matches, the request is passed to the next handler in the Connect middleware chain.

I like to combine this with quip for rapid prototyping and just getting my ideas down in code:

var Connect = require('connect'),
    quip = require('quip'),
    dispatch = require('dispatch');

var server = Connect.createServer(
    quip(),
    dispatch({
        '/': function(req, res, next, queryParams){
            var who = queryParams.name || 'world';
            res.text('hello ' + who + '!');
        },
        '/api': function(req, res, next, queryParams){
            var who = queryParams.name || 'world';
            res.json({hello: who});
        }
    })
);

server.listen(8080);

Have fun!