
Security News
Axios Maintainer Confirms Social Engineering Attack Behind npm Compromise
Axios compromise traced to social engineering, showing how attacks on maintainers can bypass controls and expose the broader software supply chain.
simple-routes
Advanced tools
A simple route pattern matcher and action associator.
npm install simple-routes
Simply import and create routes;
let Routes = require('simple-routes');
let routes = new Routes();
Add route patterns by using a string representing a url to match and an action that should match that pattern.
The url string (route pattern) should follow the minimatch documentation for matching.
Route patternss are added using an array like ['/url/route/*/to/match', action]
Action is a function callback. This function will be returned on a successful match.
let main_controller = require('./controllers/main');
// route, action
routes.addRoute('/index.html', main_controller.index);
// [route,action], [route,action]
routes.addRoutes([
['/blog/articles/*', main_controller.articles],
['**', main_controller.default]
]);
routes.hasRoute('/index.html'); // true
let action = routes.getAction('/blog/articles/cheese'); // returns main_controller.articles, undefined if not found
NOTE:
The order in which route patterns are added is important. In the example above you see the route ** which would match ANY AND ALL routes. However since it is added last the first two routes will be checked first for a match.
Duplicate route patterns will log a warning and be ignored (since they would never be reached anyway)
QUERY STRINGS:
Query strings are ignored when adding a route (addRoute(...)) and calling hasRoute(...), getAction(...), and removeRoute(...). Only the path will be considered in the match.
You can get an array of all the route patterns currently in the routes:
routes.routes();
You can get an array of all the actions currently in the routes:
routes.actions();
Get the action if the passed route string matches a route pattern. Undefined if not found:
routes.getAction('/...');
Get the route pattern that the passed route string matches. Undefined if not found:
routes.getRouteMatch('/...');
View a neater layout of the current route patterns in the router. If the function passed is a named function statement the function name will show. Otherwise the function will print.
routes.toString();
// ROUTE ACTION
// /index.html index
// /blog/article/* articles
// ** default
Named function statements (as opposed to a function literal) will help with this table. If using a function constructor then using a named function will be helpful:
Controller = new function() {
this.index = function index() {
...
};
this.create = function() {
};
};
let main_controller = new Controller();
main_controller.index.name; // index
main_controller.create.name; // ''
You can remove an individual route pattern:
routes.removeRoute('/blog/articles/**');
You can remove all routes:
routes.clearAll();
You can extend the routes using the class extend syntax available in node >6.11
const SimpleRoutes = require('simple-routes');
class MyRoutes extends SimpleRoutes {
// override functions as you wish
...
};
The following are functions and conditions that will throw errors:
addRoute(route, action)
route does not exist or is not a type string.action does not exist or is not a function.addRoutes(list)
list does not exist or is not an array.FAQs
A simple route matcher
We found that simple-routes demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Axios compromise traced to social engineering, showing how attacks on maintainers can bypass controls and expose the broader software supply chain.

Security News
Node.js has paused its bug bounty program after funding ended, removing payouts for vulnerability reports but keeping its security process unchanged.

Security News
The Axios compromise shows how time-dependent dependency resolution makes exposure harder to detect and contain.