express-custom-router
Custom routing rules for express.js.
Installation
npm install --save express-custom-router
Usage
express-custom-router
differs from the standard Express router, in that accepts a match
function in place of a route path. The mounted controller will only be called if the match
function returns true.
This allows much more flexibility in route handling.
const express = ;const CustomRouter = ; const app = ;const router = ; // Define routing rules for a controllerrouter; // Mount the routerapp;
Why?
I am working on an application with very complicated route, which can accept all sorts of different options, in all sorts of different formats. Relying on Express' routing syntax makes handling these route very difficult.
With express-custom-router
, I can parse the url however I want to, and then route based on this parsed route.
For example:
const app = ;const router = ; // Middleware for manually parsing// request parametersrouter; // Setup routes, based on my manually parsed paramsrouter;router;
Reference
CustomRouter#all(match, controller)
Mount a controller for any HTTP verb
CustomRouter#get(match, controller)
Mount a controller for GET
requests
CustomRouter#post(match, controller)
Mount a controller for POST
requests
CustomRouter#put(match, controller)
Mount a controller for PUT
requests
CustomRouter#delete(match, controller)
Mount a controller for DELETE
requests
CustomRouter#use(middleware)
Mount middleware for all requests. Middleware must use the format (req, res, next) => {}