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

routers

v0.1.4

Published

A group of router helpers for server-side use

Downloads

177

Readme

routers

A group of router helpers for server-side use

Installation

$ npm install --save routers

Included Router Helpers

apiResolver(handlerRootPathname, { dataType = 'json' })

Composes promisedResolver and handlerResolver with a response formatter based on dataType.

The resolver will also have a method named errorHandler that you can use to handle middleware errors in a standard way (uses the error resolution and data formatter from the resolver).

import express from 'express';
import { apiResolver } from 'routers';

const app = express();
const resolve = apiResolver('routes');

// resolve to module at routes/users, method index
app.get('/users', resolve('users#index'));
app.get('/users/:id', resolve('users#get'));

app.use(resolve.errorHandler);

Your routes/users.js file might look like:

function index() {
  return Users.array();
}

function get({ params: { id } }) {
  return Users.where({ id }).first();
}

export { index, get };

classApiResolver(handlerRootPathname, { dataType = 'json' })

A specialized version of apiResolver (and is used the same way) that expects a class to be exported from the resolved module. It will instantiate this class once and then each request will call the appropriate method on that class.

This is very useful when you want to use decorators, which are not available on first-class functions.

import express from 'express';
import { classApiResolver } from 'routers';

const app = express();
const resolve = classApiResolver('routes');

// resolve to module at routes/users, method index
app.get('/users', resolve('users#index'));
app.get('/users/:id', resolve('users#get'));

app.use(resolve.errorHandler);

Your routes/users.js file might look like:

import { propTypes, strip } from './magical-decorators';

class UsersRoute {
  constructor() {
    // run only once
    // will run when the first route that resolves to this module (lazy instantiation)
  }

  @strip('password')
  index() {
    return Users.array();
  }

  @propTypes({
    params: { id: '!string' }
  })
  @strip('password')
  get({ params: { id } }) {
    return Users.where({ id }).first();
  }
};

export default UsersRoute;

handlerResolver(handlerRootPathname)

Maps a root path and string to file and method.

import express from 'express';
import { handlerResolver } from 'routers';

const app = express();
const resolve = handlerResolver('routes');

// resolve to module at routes/users, method index
app.get('/users', resolve('users#index'));

promisedResolver(resolver, handleResponse, handleError)

Adapts another resolver to handle promises.

import express from 'express';
import { promisedResolver } from 'routers';

const app = express();
const resolve = promisedResolver(
  handlerResolver('routes'),
  (data, res, next) => res.send(data),
  (err, res, next) => res.statusCode(err.statusCode).send(err.stack)
);

// resolve to module at routes/users, method index
app.get('/users', resolve('users#index'));

If the handler is of the form function(req), the return value will be interpreted as a promise. Otherwise the handler acts as any normal request handler (you write to res and then call next).

guard(app, middleware, fn)

Adds a middleware to every route added inside fn.

import express from 'express';
import { guard } from 'routers';

const app = express();

guard(app, authMiddleware, (app) => {
  app.get('/users', loggedInUsers.index);
});