项目作者: microsoftarchive

项目描述 :
A simple and fast client side router that processes URL changes with middleware
高级语言: JavaScript
项目地址: git://github.com/microsoftarchive/wunderbits.router.git
创建时间: 2014-05-19T11:09:48Z
项目社区:https://github.com/microsoftarchive/wunderbits.router

开源协议:MIT License

下载


wunderbits.router

badges…

A simple and fast client side router that processes URL changes with middleware

WORK IN PROGRESS, NOT READY TO BE USED!

Getting started

First, install from npm.

  1. $ npm install --save wunderbits.router

Create a middleware function.

  1. var myMiddleware = function (req, next) {
  2. console.log('the request url is ' + req.url);
  3. next();
  4. };

Create a router instance, add the middleware and start routing.

  1. var Router = require('wunderbits.router').Router;
  2. var router = new Router();
  3. router.use(myMiddleware);
  4. router.start();

Scope middleware to certain urls and patterns

  1. // Will trigger when the url is exactly /foo/bar
  2. router.use('/foo/bar', myMiddleware);
  3. // Will trigger when the url matches the regexp
  4. router.use(/^\/foo\/bar/, myMiddleware);

Configure middleware in class definiton

  1. var Router = require('wunderbits.router').Router;
  2. var MyRouter = Router.extend({
  3. 'middleware': [
  4. myMiddleware
  5. ]
  6. });
  7. var router = new MyRouter();
  8. router.start();

Handle only specific urls

Execute a handler only when the url exactly matches a string.

  1. var MyRouter = Router.extend({
  2. 'middleware': [
  3. {
  4. "fn": myMiddleware,
  5. "url": "/foo/bar"
  6. }
  7. ]
  8. });

Pattern matching and variables

Execute a handler only when the url matches a pattern, and extract params from the url string.

  1. function myMiddleware (req, next) {
  2. console.log(req.params.id);
  3. next();
  4. }
  5. var MyRouter = Router.extend({
  6. 'middleware': [
  7. {
  8. "fn": myMiddleware,
  9. "url": /^\/foo\/:id/
  10. }
  11. ]
  12. });

Use middleware constructors

If you want to fit your middleware into a class-based system, you can define a constructors object and reference it with handlerName#methodName.

  1. var MyHandler = require('wunderbits.core').WBKlass.extend({
  2. 'doSomething': function (req, next) {
  3. // your awesome logic goes in here...
  4. next();
  5. }
  6. });
  7. var MyRouter = Router.extend({
  8. "constructors": {
  9. "foo": MyHandler
  10. },
  11. "middleware": [
  12. "foo#doSomething"
  13. ]
  14. });

A more complex example, with strict equals or pattern matching.

  1. var MyRouter = Router.extend({
  2. "constructors": {
  3. "foo": MyHandler
  4. },
  5. "middleware": [
  6. {
  7. "fn": "foo#doSomething",
  8. "url": "/foo/bar"
  9. }
  10. ]
  11. });

Develop and contribute

  1. First, fork this repo.
  2. Implement something awesome
  3. Write tests and run them with npm test
  4. Submit a pull request

Credits

Author: Adam Renklint

License

Copyright (c) 2014 6 Wunderkinder GmbH

Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the “Software”), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.