npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

method-routes

v0.1.0

Published

A simple route and method matcher for nodejs

Downloads

4

Readme

Method Routes

A simple way to match route pattern and HTTP method with an action.

Changelog

npm node deps tests coverage

Installation

npm install method-routes

Usage

Simply import and create routes;

let Routes = require('method-routes');

let routes = new Routes();

Add Routes

Add route patterns for a particular HTTP method by using a string representing a url to match and an action that will return if matched.

The url string (route pattern) should follow the minimatch documentation for matching.

Method is a standard HTTP method. One of POST, GET, PUT, PATCH, DELETE. For ease of use there are static variables available:

Routes.POST
Routes.GET
Routes.PUT
Routes.PATCH
Routes.DELETE

Route patterns use a string 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('GET:/index.html', main_controller.index);
// OR
routes.addMethodRoute(Routes.GET, '/index.html', main_controller.index);

// [method, route,action], [method, route,action]
routes.addRoutes([
  ['POST', '/blog/articles/*', main_controller.articles],
  [Routes.PUT, '**', main_controller.default]
]);

routes.hasRoute('GET:/index.html'); // true
// OR
routes.hasMethodRoute(Routes.GET, '/index.html') // also true

// returns main_controller.articles, undefined if not found
let action = routes.getAction('GET:/blog/articles/cheese');
// OR
let action = routes.getMethodAction(Routes.GET, '/blog/articles/cheese');

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.

View Routes

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 for a particular method. Undefined if not found:

routes.getAction('DELETE:/...');
// OR
routes.getMethodAction(Routes.DELETE, '/...');

Get the route pattern that the passed route string matches for a particular method. Undefined if not found:

routes.getRouteMatch('PUT:/...');
// OR
routes.getMethodRouteMatch(Routes.PUT, '/...');

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();
// METHOD     ROUTE                ACTION                        
// GET       /index.html          index                            
// POST      /blog/article/*      articles    
// PUT       **                   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; // ''

Remove Routes

You can remove an individual route pattern:

routes.removeRoute('POST:/blog/articles/*');
// OR
routes.removeMethodRoutes(Routes.POST, '/blog/articles/*');

You can remove all routes:

routes.clearAll();

Extending Routes

You can extend the routes using the class extend syntax available in node >6.11

const MethodRoutes = require('method-routes');

class MyRoutes extends MethodRoutes {
  // override functions as you wish
  ...
};

Errors

The following are functions and conditions that will throw errors:

  • addRoute(route, action)

    • action is undefined/null or is not a function.
  • addMethodRoute(method, route, action)

    • Sam as addRoute() function
  • addRoutes(list)

    • list is undefined/null or is not an array.
  • Anytime

    • method is not a string or part of POST, GET, PUT, PATCH, DELETE
    • route is undefined/null, is not a type string or does not have a valid method at the beginning with followed by a colon and the route.