A simple router used in a few of my isomorphic experiments.
npm install --save no-frills-router
var nfr = require('no-frills-router');
var http = require('http');
var router = nfr()
.map('index', '/', require('./lib/home'))
.map('profile', '/user/:username', require('./lib/profile'))
.map('*', require('./lib/not-found'), {status: 404})
;
http.createServer(function(req, res) {
//match a URL to a handler
var match = router.match(req.url);
res.status = match.status || 200;
res.end(match.handler(match, router));
}).listen(3001);
Create a new router.
Map a URL pattern to a handler.
Parameters:
[name]
- the route name - used for the.assemble()
methodpattern
- a pattern - see path-to-regex for detailshandler
- a function, object, react component or whatever you want[data]
- any other data you want to access when the route is matched
Match a URL to a handler.
Parameters:
url
- the URL
Returns:
name
- the route nameparams
- the route paramshandler
- the route handler...
- the other data you passed to the.route()
method
Assemble a URL for a handler.
name
- the route nameparams
- the route parameters
The MIT License (MIT)
Copyright (c) 2015 James Newell