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

nih-router

v3.1.0

Published

A simple router that's "Not Invented Here"

Downloads

4

Readme

nih-router

The "Not Invented Here" router. Inspired by this article this router's main distinction from the myriad of alternatives is that I started with the example code in the article and made some small tweaks.

Usage

  1. Install it with yarn or npm: npm i nih-router

  2. Import the only exposed function (resolve)

  3. Pass it your routes array, a context object (see the linked article), and a source

  4. It will run the action method of the matched route and return the result

import resolve from 'nih-router';
import routes from './routes';

resolve(routes, { pathname: '/foo' }, 'server')
    .then(([actionResult, metaResult]) => { ... });

Details

If no matching route can be found it will look for an '/error' route. If that is not found it returns a rejected promise with a 404 error.

API

resolve({Array} routes, {Object} context, {String} source)

  • routes: An array of route objects that have the following properties. The functions here are all allowed to return Promises or synchronous values.

    • path: Required: A string or array of strings to match. e.g. /users/:userid
    • action: Required: A function to run when the path is matched. Used for returning content.
    • meta: Optional: Another function to run when the path is matched. Used for returning page meta.
    • state: Optional: A function to run only on the server before the other functions run. This is used to set any needed global app state before rendering.
  • context: An object with information about the context of the request. It must have a pathname property at minimum, this object is merged with any matched path parameters and passed to the route functions.

  • source: The source of the resolve call. Only server changes behavior (calls state function on matched route). But, the data is included in the object passed to the functions in a property called source.

  • Returns: A Promise that resolves with a 2 item array (the result of the action function and the result of the meta function in that order), or rejects with an error.

License

The heart of this code is from the article linked above. That appears to be MIT licensed here. My modifications are released under the license found in this repository.