node package manager


Declarative Routing for Express.js


declarative routing for express.js

npm install express-declarative-routing

A few things on the roadmap:

  • full testing
  • cover all features of express.js routing
  1. routes are declared as nested objects, leaf values point to controller methods
  • object properties are route element names
  • '_middlware' can be used to declare what middleware is available to subroutes definitions
  • '_middlewareName1_middlewareName2' can be used to declare that subroutes are using the middleware
  • '$' in routes will be replaced with ':', which signifies a wildcard in express routing
  1. run build routes
  • this builds middleware in front of certain routes as required
  • creates a list of functions which will build the routes later
  • also conveniently outputs a table of the routes it's built and what middleware applies to each


controller = require(./controller)
module.export =
      _middleware: [ middlware 1 ] # middleware shared  by all routes in subroute_b
      get: controller.getAB
      put: controller.putAB
      post: controller.getAC
      _middleware: [ middleware1, middleware2 ] # middleware shared by all routes in groupName
          get: controller.getAD
        get: controller.getAE
        post: controller.postAE
    all: controller.getB
routing = require("./lib/routing")
app = express()
  '/mountRoute' : [
/mountRoute/route_a/subroute_b GET PUT
/mountRoute/route_a/subroute_c GET
/mountRoute/route_a/subroute_c/subroute_d/:variable GET
/mountRoute/route_a/subroute_c/subroute_e/ GET POST