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

micro-rest-fs

v0.0.4

Published

Tiny path based rest router for zeit's micro

Downloads

4

Readme

micro-rest-fs

JavaScript Style Guide

Tiny path based rest router for micro styled after next.js path based routing.

  • no config
  • parameterized paths
  • query string

usage

Assuming your have a micro server serving index.js, you can use the router like this

const micro = require('micro')

const match = require('micro-rest-fs')(__dirname)

const server = micro((req, res) => {
  const matched = match(req)
  if (matched) return matched(req, res)
  micro.send(res, 404, { error: 'Not found' })
})

server.listen(8080)

Full demo can be found in demo/simple and run with npm run demo-simple

This will allow you to create files within the api folder that map to api paths, so assuming a structure like this

demo/simple
├── api
│   ├── :echo
│   │   └── index.js
│   └── random
│       └── get.js
└── index.js

The following routes will be generated

/api/random
/
/api/:echo

The routes are sorted so that exact matches are before paramaterised matches, and specific http methods are before the general index.js handlers. Each request is handled by the first matched route.

Each route is configured to expect a function, which will be called with req and res including req.params, and req.query decorations

For example, if demo/simple/api/random/get.js looks like this

const { send } = require('micro')

module.exports = (req, res) => send(res, 200, Math.random().toString())

The api call and response will look like this

fetch('/api/random')
// => 0.006965265801673448

And where the demo/simple/api/echo/index.js file looks like this

const { send } = require('micro')

module.exports = (req, res) => send(res, 200, {method: req.method, params: req.params, query: req.query})

The call and response could look like this

fetch('/api/anything?foo=bar&baz')
// => {"method":"GET","params":{"echo":"anything"},"query":{"foo":"bar","baz":""}}

Notice the fetched url is /api/anything and is matched by the /api/:echo, and the echo param is set to the route value dynamically.

The echo route will also handle calls from unhandled http methods to /api/random which only has a GET handler based on the filename of demo/simple/api/random/get.js

fetch('/api/random?this=that', {method: 'post'})
// => {"method":"POST","params":{"echo":"random"},"query":{"this":"that"}}

Works fine with async and await

module.exports = async (req, res) => {
  const data = await fetch('http://example.com/api/something')
  send(res, 200, data)
}

If you need to control to sorting order of routes, you can pass a sort function as a config paramater, for example to give presedence to paramaterised routes over exactly matched routes you could do something like this

const match = require('micro-rest-fs')(__dirname, {sort: (a, b) => a.params.length ? -1 : 1})