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

express-route-audit

v2.0.0

Published

Audit declared Express routes with usage data

Downloads

3

Readme

express-route-audit

Audit declared Express routes with usage data.

Why

When maintaining an application, it can be useful to know what routes and methods are being used. Let the application gather usage statistics, then generate a report that lists each unique route, method and count for that combination.

How

Install the module.

npm install express-route-audit

Add to your Express application.

const ERA = require('express-route-audit');

Choose a storage backend. For transient in-memory storage that will not persist across restarts / processes, use ERAMemory.

const ERAMemory = require('express-route-audit/storage/ERAMemory');

Initialize ERA with in-memory storage.

const era = new ERA(new ERAMemory()); 

Before any routing occurs, include a middleware that upon completed requests tallies the number of requests to a given route and method.

// express-route-audit middleware; counts routed requests.
app.use((...args) => era.middleware(...args));

Then, given an Express app, a report can be generated on usage.

// Report on route usage.
app.get('/report', (request, response, next) => era.report(app)
  .then(report => response.json(report))
  .catch(error => next(error)));

The output is compatible with json2csv if JSON isn't convenient.

Example

git clone https://github.com/fluxsauce/express-route-audit.git
cd express-route-audit
npm install
node ./example.js

Then, in a new terminal:

# Get the report; the count for the report route will be 0 as it only increments after the request is complete.
» curl http://localhost:3000/report
[{"path":"/ping","method":"GET","count":0},{"path":"/hello/:target","method":"GET","count":0},{"path":"/hello","method":"POST","count":0},{"path":"/report","method":"GET","count":0},{"path":"/report","method":"DELETE","count":0}]% 

Formatted:

[  
   {  
      "path":"/ping",
      "method":"GET",
      "count":0
   },
   {  
      "path":"/hello/:target",
      "method":"GET",
      "count":0
   },
   {  
      "path":"/hello",
      "method":"POST",
      "count":0
   },
   {  
      "path":"/report",
      "method":"GET",
      "count":0
   },
   {  
      "path":"/report",
      "method":"DELETE",
      "count":0
   }
]
# Make some requests to populate the stats.
» curl http://localhost:3000/ping
PONG%                                                                                                     
» curl http://localhost:3000/ping
PONG%
» curl http://localhost:3000/ping
PONG%
» curl http://localhost:3000/hello/world
world%
» curl -d "foo=bar&fizz=buzz" -X POST http://localhost:3000/hello
{"foo":"bar","fizz":"buzz"}%
» curl http://localhost:3000/fail
Not Found%
» curl http://localhost:3000/report
[{"path":"/ping","method":"GET","count":3},{"path":"/hello/:target","method":"GET","count":1},{"path":"/hello","method":"POST","count":1},{"path":"/report","method":"GET","count":0},{"path":"/report","method":"DELETE","count":0}]%
# Delete the report.
» curl -X DELETE http://localhost:3000/report
» 

Formatted:

[  
   {  
      "path":"/ping",
      "method":"GET",
      "count":3
   },
   {  
      "path":"/hello/:target",
      "method":"GET",
      "count":1
   },
   {  
      "path":"/hello",
      "method":"POST",
      "count":1
   },
   {  
      "path":"/report",
      "method":"GET",
      "count":0
   },
   {  
      "path":"/report",
      "method":"DELETE",
      "count":0
   }
]

Example server output:

» node ./example.js 
listening on 3000
2019-03-18T16:50:17.342Z 'GET' '/report'
2019-03-18T16:50:21.567Z 'GET' '/ping'
2019-03-18T16:50:22.104Z 'GET' '/ping'
2019-03-18T16:50:22.626Z 'GET' '/ping'
2019-03-18T16:50:26.241Z 'GET' '/hello/world'
2019-03-18T16:50:31.809Z 'POST' '/hello'
2019-03-18T16:50:36.978Z 'GET' '/fail'
2019-03-18T16:50:42.197Z 'GET' '/report'
2019-03-18T16:50:47.603Z 'DELETE' '/report'

Redis

The storage/ERARedis backend takes two arguments; a fully configured Redis client and a key prefix. Tested with ioredis.

Example:

// Initialize ERA with Redis storage.
const era = new ERA(new ERARedis(redis, 'era::'));