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 🙏

© 2026 – Pkg Stats / Ryan Hefner

yaf-router

v0.3.5

Published

Yet Another F-ing Router

Readme

Yet Another F@#%ing Router

npmnpm type definitionsSnyk Vulnerabilities for npm packagenpmlast commit

A basic HTTP-Router with zero dependencies. Made for NodeJS-HTTP(s)-Server but the route-handling only depends on request.url and request.method so it should work with anything. The Middlewares require response.write(), response.end() and response.writableEnded.

Getting started

const NodeHttp = require('http');
const {Router} = require('yaf-router')

const router = new Router()

// GET /bestUser/foobar => "Hello bestUser, foobar"
router.addRoute('/{user}/{msg}', ({parameter, response}) => {
  response.write(`Hello ${parameter.user}, ${parameter.msg}`)
  response.end()
})

const server = NodeHttp.createServer((request, response) => {
  router.handle(request, response)
});
server.listen(8080);

Documentation

Routes

Middlewares

Interfaces

HandlerData

The HandlerData stores the Parameter, Request, Response and almost always the written data.

interface HandlerData {
    parameter: {[key: string]: string},
    request: NodeHttp.IncomingMessage,
    response: NodeHttp.ServerResponse,
    written?: Array<any>
}

Routes

A Route consists of a Method and URL: GET /hallo/User. The Method is optional and defaults to GET.

router.addRoute('/me', ({parameter, response}) => {
	// ...
  response.end()
})
// is the same as 
router.addRoute('GET /me', ({parameter, response}) => {
	// ...
  response.end()
})

You can define multiple Routes for the same Handler by passing an Array of Routes

router.addRoute(['/me', 'POST /you'], ({parameter, response}) => {
	// handles: GET /me and POST /you
  response.end()
})

Parameter

Parameter are part of the URL like /search/{query} .

// GET /search/foobar => "Searching: foobar"
router.addRoute('/search/{query}', ({parameter, response}) => {
  response.write(`Searching ${parameter.query}`)
  response.end()
})

Order and count of Parameter do not matter. There may be multiple Paramater in a row.

// GET /user/bestUser/foobar => "Hello bestUser, foobar"
router.addRoute('/user/{user}/{msg}', ({parameter, response}) => {
  response.write(`Hello ${parameter.user}, ${parameter.msg}`)
  response.end()
})

Wildcard-Parameter are defined by the URI-Part /* and have to come last. Wildcard-Parameter are accessed by the *-Key. Normal Parameter are prioritized and matched first.

// GET /wildcard/BestUser/foo/bar => "BestUser is trying to access foo/bar"
router.addRoute('/wildcard/{user}/*', ({parameter, response}) => {
  response.write(`${parameter.user} is trying to access ${parameter['*']}`)
  response.end()
})

If there are multiple Parameter with the same key in a URL only the last value will be used. In the future this case may be handled as an Array.

// GET /search/foo/spacing/bar => "Searching: bar"
router.addRoute('/search/{search}/spacing/{search}', ({parameter, response}) => {
  response.write(`Searching: ${parameter.search}`)
  response.end()
})

Middleware

A Middleware implements the MiddlewareInterface. Middlewares are called after routing so it is not possible to reroute via Middlewares.

class CorsMiddleware implements MiddlewareInterface {
  getTypes() {
    return [
      MiddlewareOrderType.Header
    ]
  }
  
  getName() {
    return 'CorsMiddleware'
  }
  
  async handleByType(type: MiddlewareOrderType, handlerData: HandlerData, path: string = '') {
    if(type !== MiddlewareOrderType.Header) {
      return
    }
    handlerData.response.setHeader('Access-Control-Allow-Origin', '*');
		handlerData.response.setHeader('Access-Control-Request-Method', '*');
		handlerData.response.setHeader('Access-Control-Allow-Methods', 'OPTIONS, GET');
		handlerData.response.setHeader('Access-Control-Allow-Headers', '*');
  }
}

The order of Middleware execution and their suggested usage is:

  1. Security: Makes security checks and ends the response if needed
  2. Header: Only adds header to the response
  3. Data: Enriches the HandlerData with additional information
  4. Result: Manipulates the Response
  5. Finisher: Can be used for caching for example

Every Handler can end the Response but only expected should do so.

Included Middlewares

Cache

A simple Cache implementation. Uses MiddlewareOrderType.Data if cache hit and MiddlewareOrderType.Finisher to fill cache. Default Cache-TTL is 60 seconds. Only caches GET, OPTIONS and HEAD methods.

Cors

A simple Middleware that adds CORS-Header.