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

router-dex

v1.1.0

Published

Route and Middleware inspector for Express >= 4

Downloads

16

Readme

Installation

yarn add router-dex -D | npm i router-dex -D

Requirements

Express >= 4

Integration as module script

package.json

{
  "name": "your-project",
  "version": "1.0",
  "main": "index.js",
  "scripts": {
    "route:list": "node -r router-dex src/app.js default"
  },
}

Just add the relative pathname to the file where your express application is exported.

If your application is exported under a namespace instead of default, then change it.

Typescript

"scripts": {
  "route:list": "ts-node -r router-dex src/app.ts default"
},

If you are using typescript custom paths then use tsconfig-paths

"route:list": "ts-node -r tsconfig-paths/register -r router-dex src/app.ts default"

It is important to require router-dex last.

Integration inside a script file

This implementation allows to you to do some stuffs before and after router-dex is executed.

src/scripts/route-list.js

const routerDex = require("router-dex/inspector")
const app = require("../app")

routerDex(app, "My App")

Then add the script to your package.json

{
  "name": "your-project",
  "version": "1.0",
  "main": "index.js",
  "scripts": {
    "route:list": "node src/scripts/route-list.js"
  },
}

Typescript

import routerDex from "router-dex/inspector"
import app from "../app"

routerDex(app, "My Typescript App")

package.json

"route:list": "ts-node src/scripts/route-list.ts"

If you are using typescript custom paths then use tsconfig-paths

"route:list": "ts-node -r tsconfig-paths/register src/scripts/route-list.ts"

Usage in any javascript file

Also is possible to get all the routes in a object notation format

const { getAllRoutes } = require("router-dex/inspector")
const app = require("./src/app")

const { routes, sortedRoutes } = getAllRoutes(app)

You will get all the routes and all of them sorted by alphabet + base path + without parameters first.

Example

{
  routes: [
    { path: '/store', method: 'GET', middlewares: [Array] },
    { path: '/products', method: 'GET', middlewares: [Array] },
    { path: '/users', method: 'GET', middlewares: [Array] }
  ],
  sortedRoutes: [
    { path: '/products', method: 'GET', middlewares: [Array] },
    { path: '/store', method: 'GET', middlewares: [Array] },
    { path: '/users', method: 'GET', middlewares: [Array] }
  ]
}

Typescript

import { getAllRoutes, DexRoute } from "router-dex/inspector"
import app from "./src/app"

const { routes }: { routes: DexRoute[] } = getAllRoutes(app)

Command-line filtering

Router Dex groups all routes by default. To filter by type, just add it at the end of the script call

yarn route:list store | npm run route:list store

Also is possible to pass multiple

yarn route:list store products | npm run route:list store products

Types passed must match exactly with the group name.

Symbols and meanings

In the middleware column of the generated table you can see the following notations:

  • *: means the middleware/controller function passed is binded controller.bind(ref)
  • anonymous: means the middleware/controller function was passed directly into the router/app

Inside the middleware array of each route returned by getAllRoutes you can see the following values:

  • bound middlewareName: means the middleware/controller function passed is binded
  • <anonymous>: means the middleware/controller function passed is anonymous

Considerations: Avoid anonymous middlewares!

Instead of

router.get("/products", (req, res) => {
  res.json({ status: 200 })
})

Use

const { getProducts } = require("./src/controllers/products")
router.get("/products", getProducts)

Reason why is when you are passing the middleware function directly into the app/router, you are losing track of the reference name to the function itself. This ocurrs because you are not storing it at memory in first place.

In that case you will receive a command-line warning like

* Do not use anonymous functions as middlewares, store them in a constant instead.

License

MIT. Read the license


label