tree-route is a performance focused request router with regular expressions support.
Install the latest version with npm install --save tree-route
Basic usage:
var Router = require('tree-route');
var router = new Router();
// Defining route for several HTTP methods
router.addRoute(['GET', 'POST'], '/', 'handler0');
// Defining route for one HTTP method
router.addRoute('GET', '/news', 'handler1');
// Defining route with regular expression param
router.get('/news/{id:[0-9]+}', 'handler2');
// Defining another route with regular expression param
router.get('/news/{slug:[a-zA-Z\-]+}', 'handler3');
// Defining static route that conflicts with previous route, but static routes have high priority
router.get('/news/all', 'handler4');
// Defining another route
router.post('/news', 'handler5');
var method = 'GET';
// Optionally pass HEAD requests to GET handlers
// if (method == 'HEAD') {
// method = 'GET';
// }
var url = '/news/1';
var result = router.dispatch(method, url);
if (typeof result.error === 'undefined') {
var handler = result.handler;
var params = result.params;
// Do something with handler and params
} else {
switch (result.error.code) {
case 404 :
// Not found handler here
break;
case 405 :
// Method not allowed handler here
var allowedMethods = result.allowed;
if (method == 'OPTIONS') {
// OPTIONS method handler here
}
break;
}
}
Save and restore routes (useful for routes caching):
var Router = require('tree-route');
var router = new Router();
router.addRoute(['GET', 'POST'], '/', 'handler0');
router.addRoute('GET', '/news', 'handler1');
var routes = router.getRoutes();
var anotherRouter = new Router();
var anotherRouter.setRoutes(routes);
var method = 'GET';
var url = '/news';
var result = anotherRouter.dispatch(method, url);