1.3.4 • Public • Published

Build Status Quality Gate Status Open Source Helpers FOSSA Status

npm version install size npm downloads GitHub license

anyroute - lightweight router works anywhere

A flexible lightweight router you can use in nodejs and browser. No dependency.


npm i anyroute

Or find help from:


const {Anyroute, MatchResult} = require('anyroute');
const anyroute = new Anyroute;

anyroute.set('/happy/:foo/and/:bar', (params) => { console.log("Happy " + + " and " + ); return +; });

let foobar = anyroute.get('/happy/trees/and/kitties').run();
// Happy trees and kitties
// foobar: treeskitties

anyroute.set('/:aaa/:bbb', (match) => {return match;})
        .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.and;


Define a route and placeholder. Returns error (if any), the handler been set, and an empty payload.

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


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( );.

Also can do run(additionalParams) and run(additionalParams, callback).

var result = ar.get("/collection/:cid/tab/:tabID").run();

var ret = ar.get("/collection/:cid/tab/:tabID", {}, "default");
let returnedByHandler =;
// also can have custom payload here as first argument
// similar to ret.handler(ret.payload)

let cid ={foo: bar}, (x) => x.cid);
let tab = => 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).

// setup `notfound` handler
anyroute.notfound(function (matchResult) {
        // call when NO exact match found
        // matchResult is an MatchResult Object
        return +;


MatchResult {
  err: undefined,
  handler: [Function: handler],
  payload: { foo: 'forty', bar: 'bobs', and: 'adam' },
  default: undefined }


FOSSA Status

Package Sidebar


npm i anyroute

Weekly Downloads






Unpacked Size

21 kB

Total Files


Last publish


  • bluet