https://github.com/digitaledgeit/no-frills-router
A simple router used in a few of my isomorphic experiments.
https://github.com/digitaledgeit/no-frills-router
Last synced: 11 months ago
JSON representation
A simple router used in a few of my isomorphic experiments.
- Host: GitHub
- URL: https://github.com/digitaledgeit/no-frills-router
- Owner: digitaledgeit
- Created: 2015-09-20T11:26:45.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2015-10-20T10:05:51.000Z (over 10 years ago)
- Last Synced: 2025-02-25T12:03:22.187Z (over 1 year ago)
- Language: JavaScript
- Size: 125 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# no-frills-router
A simple router used in a few of my isomorphic experiments.
## Installation
npm install --save no-frills-router
## Usage
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);
## API
### new Router()
Create a new router.
### .map([name : string,] pattern : string, handler : mixed [, data : object])
Map a URL pattern to a handler.
Parameters:
- `[name]` - the route name - used for the `.assemble()` method
- `pattern` - a pattern - see [path-to-regex](https://www.npmjs.com/package/path-to-regexp) for details
- `handler` - a function, object, react component or whatever you want
- `[data]` - any other data you want to access when the route is matched
### .match(url : string) : object|null
Match a URL to a handler.
Parameters:
- `url` - the URL
Returns:
- `name` - the route name
- `params` - the route params
- `handler` - the route handler
- `...` - the other data you passed to the `.route()` method
### .assemble(name : string [, params : Object]) : string|null
Assemble a URL for a handler.
- `name` - the route name
- `params` - the route parameters
## License
The MIT License (MIT)
Copyright (c) 2015 James Newell