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

@olavgm/serverless-router

v1.1.3

Published

Router for serverless functions

Readme

Serverless Router

Serverless Router is a routing module for serverless functions. It routes paths to methods.

Installation

npm

npm install @olavgm/serverless-router

Usage

const Router = require('@olavgm/serverless-router')

let options = {}

Router.get('/hello/:name', options, async (req, res, authInfo) => {
  console.info(`Sending response to ${req.method} ${req.path}`)
  res.status(200).send(`Hello, ${req.params.name}, ${req.method} ${req.path}`)
})

Router.post('/hello/:name', options, async (req, res, authInfo) => {
  console.info(`Sending response to ${req.method} ${req.path}`)
  res.status(200).send(`Hello, ${req.params.name}, ${req.method} ${req.path} ${req.body.age}`)
})

// Registers for all HTTP verbs
Router.all('/goodbye/:name', options, async (req, res, authInfo) => {
  console.info(`Sending response to ${req.method} ${req.path}`)
  res.status(200).send(`Goodbye, ${req.params.name}`)
})

exports.testgcfapi = (req, res) => {
  Router.route(req, res)
}

It could also be done this way, using the register method and passing the verb as the first parameter.

const Router = require('@olavgm/serverless-router')

Router.register('GET', '/hello/:name', options, async (req, res) => {
  console.info(`Sending response to ${req.method} ${req.path}`)
  res.status(200).send(`Hello, ${req.params.name}, ${req.method} ${req.path}`)
})

Router.register('POST', '/hello/:name', options, async (req, res, authInfo) => {
  console.info(`Sending response to ${req.method} ${req.path}`)
  res.status(200).send(`Hello, ${req.params.name}, ${req.method} ${req.path} ${req.body.age}`)
})

Router.register('*', '/goodbye/:name', options, async (req, res, authInfo) => {
  console.info(`Sending response to ${req.method} ${req.path}`)
  res.status(200).send(`Goodbye, ${req.params.name}`)
})

exports.testgcfapi = (req, res) => {
  Router.route(req, res)
}

In this example, three routes are registered, GET /hello/:name, POST /hello/:name and /goodbye/:name.

options is an object that can contain a property authorization, which is a function (req => ()) that will check for authorization. The function receives the request object as the only parameter. It should return false is the authorization is not valid, and whatever information if the authorization is valid. For example, in an JWT bearer token validation, the function can return the decoded JWT.

The exports.testgcfapi is the method that will be called by Google Cloud Functions. For deploying this function this method has to be specified:

gcloud functions deploy testgcfapi --runtime nodejs8 --trigger-http

In the exports.testgcfapi method the routing of the request is executed to process the request through the registered routes.

Static routes

If you want to include static files use this, where the paramameter is the path to the folder where the static files are.

Router.static(`${__dirname}/static`)

The router will try to match the registered URLs. If none of the match, it will try to find the file in the static folder, including route. If so, it will return the file.

In this example, if you have the file ./static/docs/about.html, the route /docs/about.html will return the file.