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

koa-fs-router

v0.7.0

Published

FS Router implementation for KoaJS

Downloads

761

Readme

koa-fs-router

Use the FS as your KOA router

"features"

  • ✅ 0 runtime dependencies
  • ✅ < 100 loc
  • ✅ little or no config
  • ✅ parameterized paths
  • ✅ parses query string

Introduction

I have always enjoyed using my filesystem as a router, settings up directories and files and automatically mapping them into a route.

After seeing the success of Zeit, and their zero-config fs as a router implementation as well, I wanted to use a lirary for KoaJS that would automatically take a directory and make a router from its hierarchy.

I found fs-router that did this exact same thing for micro, so I ported over his awesome library and made it work for KoaJS

Usage

router usage

// index.js
const koa = require('koa')
const router = require('koa-fs-router')(__dirname + '/routes')
const app = new koa()

app.use(router)
app.listen(3000)

The above usage assumes you have a folder called routes next to the index.js file, that looks something like this:

routes/
├── foo
│   └── :param
│       └── thing.js
└── things
    └── :id.js

the above tree would generate the following routes:

/foo/:param/thing
/things/:id

defining a route

// routes/foo/bar.js
module.exports = async (ctx) => {
	ctx.body = "Testing..."
}

path parameters

// routes/foos/:id.js
const { send } = require('micro')

// responds to any method at /foos/* (but not /foos or /foos/bar/baz)
module.exports = async function(req, res) {
  // params are always required when in a path, and the
  send(res, 200, { id: req.params.id })
}

priority

module.exports.GET = async (ctx) => {
	ctx.body = "Testing..."
}
// all routes are sorted by this property - the higher numbers are matched first.
// kind of like a z-index for your routes.
// note that equal priority will just sort based on the fs in the case of a collision, which is not guaranteed order on OSX/Linux
module.exports.priority = -1

custom path

// routes/whatever.js
module.exports.GET = async (ctx) => {
	ctx.body = "Testing..."
}
// exposing a "path" will override the fs-generated one.
// This is nice if you wanted to avoid making a really deep tree for a one-off path (like for oauth callbacks)
// or if you just want to avoid putting `:` in your file/folder names or something
module.exports.path = '/foo/bar'

index routes

// routes/index.js
module.exports.GET = async (ctx) => {
	ctx.body = "Testing..."
}
// The above route would be reachable at / and /index.
// This works for deep paths (/thing/index.js maps to /thing and /thing/index)
// and even for params (/thing/:param/index.js maps to /thing/* and /thing/*/index).

filter routes

// index.js
const { send } = require('micro')

// set up config to filter only paths including `foo`
const config = {filter: f => f.indexOf('foo') !== -1}

// pass config to `fs-router` as optional second paramater
let match = require('fs-router')(__dirname + '/routes', config)

module.exports = async function(req, res) {
  let matched = match(req)
  if (matched) return await matched(req, res)
  send(res, 404, { error: 'Not found' })
}

The above usage assumes you have a folder called routes next to the index.js file, that looks something like this:

routes/
├── foo
│   ├── index.js
│   └── thing.js
└── bar
    ├── index.js
    ├── foo.js
    └── thing.js

the above tree would generate the following routes:

/foo
/foo/thing
/bar/foo