Giter Site home page Giter Site logo

dispatch's Introduction

Dispatch

A really simple URL dispatcher for Connect. 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){
            ...
        },
        '/user/:id': function(req, res, next, id){
            ...
        },
        '/user/posts': function(req, res, next){
            ...
        },
        '/user/posts/(\\w+)': function(req, res, next, post){
            ...
        }
    })
);

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){ ...  },
        '/user': {
            '/': function(req, res, next){ ...  },
            '/posts': function(req, res, next){ ...  },
            '/posts/(\\w+)': function(req, res, next, post){ ...  }
        }
    })
);

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){ ... },
        '/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){ ... },
            'POST /item': function(req, res, next){ ... },
        }
    })
);

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) {
            ...
        },
        POST: function (req, res, next) {
            ...
        }
    }
})

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){
            res.text('hello world!');
        },
        '/api': function(req, res, next){
            res.json({hello: 'world'});
        }
    })
);

server.listen(8080);

Have fun!

dispatch's People

Stargazers

Chris Ennis avatar

Watchers

Chris Ennis avatar James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.