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-analysis

v1.0.1

Published

Analyze Express routes

Downloads

4

Readme

Express Route Analysis

Acts as a wrapper over Express to analyze application routes.

It handles routes that are defined with .VERB or .all (i.e. not these defined with .use).

All the time variables are in milliseconds.

Example

See example.js for a complete example. It is an Express server with a REST service that can do amazing things, but for now it only delivers random numbers. Well, it's just an example.

The routes are defined with a description, which is impossible to do with a regular router.

The service has an introspective route that can display the defined routes (in the service only).

Route statistics are taken by intervals of five minutes and stored in a Couchbase database. Statistics are computed with statsjs.

Route descriptor

In this package, a Route is an object with following fields:

  • mountpath (String) is the mount path, in case the route is inside a router and has been used with a mount path, or is an empty string
  • path (String) is the defined path
  • method (String) is the HTTP verb in lowercase or all
  • data (Object) is an opaque user object where you can store any information you want

When watched through a Stats object, a Route has also a field metrics with the following fields:

  • errors (Array<Error>) contains all the errors thrown from the route
  • statusCode (Object<Number>) contains the number of responses for each status code
  • times (Array<Number>) contains the times taken to complete the requests that have ended or failed in the route

.Router(expressRouter [, callback])

Returns a drop-in wrapper over expressRouter that can define routes to watch. It transmits actually every function call to expressRouter, moreover the .VERB and .all functions define the routes to watch.

If defined, callback is called every time a route is added to the router through .VERB or .all, with the following arguments:

  1. route (Route) being added
  2. middlewares (Array) declared

and must return an Array of middlewares. Therefore, callback can be used to filter the route middlewares.

It is impossible to wrap the application router, so if you want to analyze routes defined at the application level (app.VERB and app.all), define them on a proxy router and app.use(proxyRouteur) instead.

.routes

Array<Route>

Routes defined in this router.

.Stats(expressApp [, callback])

Returns an object that watches routes. Call stats.use instead of app.use to define Routers to watch.

.start(interval, callback)

Start to watch routes. callback is called every interval ms with the following arguments:

  1. elapsedTime(Number) is the elapsed time since last call to callback (may somewhat differ from interval)

Ensure that the execution time of callback is lower than interval.

.stop()

Stop to watch routes.

.use([mountpath,] router...)

Replacement for app.use. It is mandatory to call this method to watch Routers. Other middlewares can be used through this method or app.use interchangeably.

.routes

Array<Route>

Routes defined in the routers used by this Stats.

Helper middlewares

Debug middlewares provided for convenience.

.delay(time)

Wait time ms, and pass the control to the next handler.

router.get('/processing', routeAnalysis.delay(1000), function(req, res) {
	return res.send("Hard work is done.");
});

.mayFail(ratio [, statusCode = 503])

Send statusCode with a probability ratio between 0 (never) and 1 (always), otherwise pass the control to the next handler. Despite the name, statusCode doesn't have to be an error status code.

router.get('/index.coffee', routeAnalysis.mayFail(0.1, 418), function(req, res) {
	return res.sendFile('index.coffee');
});

License

Copyright (c) 2014 Bloutiouf aka Jonathan Giroux

MIT licence