https://github.com/bluet/anyroute.js
A flexable lightweight router you can use in nodejs and browser. No dependency.
https://github.com/bluet/anyroute.js
browser dispatcher fossa-status hacktoberfest handler nodejs path payload router shortcut uri-parser url
Last synced: 4 months ago
JSON representation
A flexable lightweight router you can use in nodejs and browser. No dependency.
- Host: GitHub
- URL: https://github.com/bluet/anyroute.js
- Owner: bluet
- License: isc
- Created: 2016-04-30T08:53:17.000Z (about 10 years ago)
- Default Branch: master
- Last Pushed: 2025-11-25T11:25:29.000Z (7 months ago)
- Last Synced: 2025-11-28T17:05:18.119Z (7 months ago)
- Topics: browser, dispatcher, fossa-status, hacktoberfest, handler, nodejs, path, payload, router, shortcut, uri-parser, url
- Language: JavaScript
- Homepage:
- Size: 188 KB
- Stars: 10
- Watchers: 1
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[](https://github.com/bluet/anyroute.js/actions/workflows/node.js.yml)
[](https://sonarcloud.io/dashboard?id=anyroute)
[](https://www.codetriage.com/bluet/anyroute.js) [](https://app.fossa.io/projects/git%2Bgithub.com%2Fbluet%2Fanyroute.js?ref=badge_shield)
[](https://www.npmjs.org/package/anyroute)
[](https://packagephobia.now.sh/result?p=anyroute)
[](http://npm-stat.com/charts.html?package=anyroute)
[](https://github.com/BlueT/anyroute.js/blob/master/LICENSE)
# anyroute - lightweight router works anywhere
A flexible lightweight router you can use in nodejs and browser. No dependency.
# INSTALL
`npm i anyroute`
Or find help from:
- https://www.npmjs.com/package/anyroute
- https://github.com/BlueT/anyroute.js
## SYNOPSIS
~~~~ js
const {Anyroute, MatchResult} = require('anyroute');
const anyroute = new Anyroute;
anyroute.set('/happy/:foo/and/:bar', (params) => { console.log("Happy " + params.foo + " and " + params.bar ); return params.foo + params.bar; });
let foobar = anyroute.get('/happy/trees/and/kitties').run();
// Happy trees and kitties
// foobar: treeskitties
anyroute.set('/:aaa/:bbb', (match) => {return match;})
.get('/doraemon/superman')
.run({'c':'c'}, (result) => console.log(result));
// { 'aaa': 'doraemon', 'bbb': 'superman', 'c': 'c' }
anyroute.notfound(function (matchResult) {
// call when NO exact match found
// matchResult is an MatchResult Object
return matchResult.payload.foo + matchResult.payload.and;
});
~~~~
## Setter
Define a route and placeholder.
Returns error (if any), the handler been set, and an empty payload.
~~~~ js
function handler () {};
function handler_post () {};
anyroute.set('/collection/:cid/tab/:tabID', handler);
// If no feat (a tag) has been set, means 'default'.
var ret = anyroute.set('/collection/:cid/tab/:tabID', handler, 'default');
// When a handler of a feat has previously been set,
// it'll overwrite with the new, and return a message in err.
// Returns modified anyroute itself, too.
var ret = anyroute.set('/collection/:cid/tab/:tabID/', handler_post, 'post')
// feat can be anything, just like a tag
~~~~
## Getter
~~~~ js
var ret = anyroute.get('/collection/123/tab/456');
// If no feat (a tag) has been set, means 'default'.
// Returns MatchResult object
var ret = anyroute.get('/collection/ccc/tab/ttt', {user: 'keroro'});
// You can put user's payload, and they'll be merged into one in return.
// Custom payload with the same name will be override by get().
// ret.payload: { user: 'keroro', cid: 'ccc', tabID: 'ttt' }
var ret = anyroute.get('/collection/foo/tab/bar', {cid: 'admin'}, 'all');
// Also the 'all' means return all handlers from all feats.
// ret.handler:
// { default: [Function: handler],
// post: [Function: handler_post] }
var ret = anyroute.get('/collection/abc/tab/xyz', {}, 'head');
// Getting handler of a feat you've never set before,
// will return the default handler, with a error message.
// So you can have the fallback if you want.
~~~~
### run() shortcut
Call with _run([object?: payload, function?: callback])_
`run()` is a shortcut of `MatchResult.handler( MatchResult.payload )`.
`run(callback)` is a shortcut of `callback( MatchResult.run() );`.
Also can do `run(additionalParams)` and `run(additionalParams, callback)`.
~~~~ js
var result = ar.get("/collection/:cid/tab/:tabID").run();
var ret = ar.get("/collection/:cid/tab/:tabID", {}, "default");
let returnedByHandler = ret.run(req.body.data);
// also can have custom payload here as first argument
// similar to ret.handler(ret.payload)
let cid = ret.run({foo: bar}, (x) => x.cid);
let tab = ret.run((x) => x.tabID);
// additional processing on data returned by pre-set handler
~~~~
Before calling `run()`, you can set `.notfound(handleNotFound)` handler, which will be called if err occurred (path not found).
Function _handleNotFound_ will be called with _MatchResult_ as input parameter. `handleNotFound(MatchResult)`.
~~~~ js
// setup `notfound` handler
anyroute.notfound(function (matchResult) {
// call when NO exact match found
// matchResult is an MatchResult Object
return matchResult.payload.foo + matchResult.payload.bar;
});
~~~~
### MatchResult
~~~~ js
MatchResult {
err: undefined,
handler: [Function: handler],
payload: { foo: 'forty', bar: 'bobs', and: 'adam' },
default: undefined }
~~~~
## License
[](https://app.fossa.io/projects/git%2Bgithub.com%2Fbluet%2Fanyroute.js?ref=badge_large)