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 🙏

© 2025 – Pkg Stats / Ryan Hefner

app-router

v0.1.3

Published

Routing for expressjs

Downloads

63

Readme

NPM   Build Status

app-router

routing for expressjs

usage
var express= require("express");
var app = express();
var router = require("app-router");
router(app).setCWD(__dirname).route("./routes/route.json");

Set current working directory (CWD) relative to all controllers path and route.json

setCWD(__dirname)

Throwing Error on unresolved action of route (default is true)

throwErrorOnWrongActionPath(false)

console.log all routing mapped by app-router (default is false)

logRoute(true)

Attach action manually to route in application (useful for other frameworks) (optional) (not required in express)

letMeAttach(function(method, route, actions){
    // method {String} = get, post, delete etc
    // route {String} = given by user or created by resource eg /user
    // actions {Array} = an array of actions resolved
    // routing example in express
    actions.unshift(route);
    app[method].apply(app, actions);

})

Use .format route in resource routing , by falsing it no route for .format will attach to resource routing. (default is true)

useFormatInResource(false)

sample route.json

{
  "VARIABLE":{
    "cp":"./controllers/my_controller",
    "other_controller":"./controllers/other_controller.js"
  },
  "GET":{
    "/user" : ["{cp}:myMiddleWareFunction" , "{cp}:myMethod"],
    "/user/:id" : "{cp}:myClass.myMethod"
  },
  "POST":{
    "/hello" : "./controllers/my_controller:createApp"
  },
  "PUT":{
    "/hello" : "{other_controller}:helloPut"
  },
  "DELETE":{
    "/user" : "./controllers/other_controller.js:destroyApp"
  },
  "RESOURCE":{
    "/res"  : "{cp}",
    "/user" : "./controllers/user_controller.js"
  }
}

Resource action mapping

Actions are mapped accordingly:


"RESOURCE":{
  "/user" : "./controllers/user_controller"
}

Method  Route                       Action (in controller ./controllers/user_controller.js)

GET     /user                       ->  index
GET     /user.:format               ->  index
GET     /user/new                   ->  new
GET     /user/new.:format           ->  new
POST    /user                       ->  create
POST    /user.:format               ->  create
GET     /user/:id                   ->  show
GET     /user/:id.:format           ->  show
GET     /user/:id/edit              ->  edit
GET     /user/:id/edit.:format      ->  edit
PUT     /user/:id                   ->  update
PUT     /user/:id.:format           ->  update
DELETE  /user/:id                   ->  destroy
DELETE  /user/:id.:format           ->  destroy

Basic syntax for route.json

{"/app_route" : [ "path_to_controller:method_name" ]}

OR

{"/app_route" : [ "path_to_controller:class_name.method_name" ]}

OR

{"/app_route" : [
  "path_to_controller:middleware_one", 
  "path_to_controller:middleware_two", 
  "path_to_controller:method_name" 
  ]
}

Yaml routing in app-router

router(app).setCWD(__dirname).route("./routes/route.yml");

sample route.yml


VARIABLE: 
  cp: "./controllers/my_controller"
  other_controller: "./controllers/other_controller.js"
GET: 
  /user: 
    - "{cp}:myMiddleWareFunction"
    - "{cp}:myMethod"
  /user/:id: "{cp}:myClass.myMethod"
POST: 
  /hello: "./controllers/my_controller:createApp"
PUT: 
  /hello: "{other_controller}:helloPut"
DELETE: 
  /user: "./controllers/other_controller.js:destroyApp"
RESOURCE: 
  /res: "{cp}"
  /user: "./controllers/user_controller.js"

Text routing in app-router

router(app).setCWD(__dirname).route("./routes/route.txt");

sample route.txt


VARIABLE  cp                    ./controllers/my_controller

VARIABLE  other_controller      ./controllers/other_controller.js

GET       /user_controller      {cp}:myMiddleWareFunction   {cp}:myMethod

GET       /user/:id             ./controllers/other_controller:myClass.myAction

POST      /hello                ./controllers/my_controller:createApp

PUT       /hello                ./controllers/my_controller:createApp

DELETE    /user                 ./controllers/other_controller.js:destroyApp

RESOURCE  /res                  {cp}

RESOURCE  /user                 ./controllers/user_controller.js