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

aws-simple-lambda-router

v0.0.3

Published

Class to route apigateway requests inside lambda

Downloads

8

Readme

Lightweight promise based router for AWS Lambda invoked by AWS Apigateway lambda proxy integration

Inspiration

Usage

Define your common middleware, routes, and default route.

const Router = require('lambda-router');
const routes = require('./routes'); // your routes
const authorizer = require('./authorizer'); // your authorizer
let router = new Router({
  middleware: [
  	middleware1,
  	middleware2
  ],
  routes: [
  	[ 'GET', '/widgets', routes.getWidgets ],
  	[ 'GET', '/widgets/:widgetId', [ authorizer.widget.get, routes.getWidget ] ],
  	[ 'PUT', '/widgets/:widgetId', [ authorizer.widget.put, routes.putWidget ] ]
  ],
  defaultRoute: routes.defaultRoute
});

Use the router

module.exports.handler = function (event, context, callback) {
  event._context = context // lambda-router only passes the event object. I do this if I need the context object.
  return router.route(event)
  .then(result => {
  	// post route processing
  	callback(null, result)
  })
  .catch(err => {
  	// handle the error here
  })
}

Middleware

Middleware is a function that takes the lambda event as a parameter, performs some work, including modifying the event and returns the event (or another object). The result is passed to the next middleware function. Middleware can return a promise of the event object or the event object.

Middleware errors/rejections are not handled.

How it works

Each common middleware function is called in turn with the results from the preceeding middleware. The first middleware function is called with the event object passed into router.route. After the common middleware executes, the correct route is determined using the path-to-regexp.

The path parameters are added to the event.pathParameters object to make migrating from apigateway routing simpler.

After the route is determine the route's middleware is called in the same manner as the common middleware. Finally the route handler is called.

Using the above example router configuration and the request GET /widgets/1234 router in effect makes these calls

Promise.resolve(event)
.then(middleware1)
.then(middleware2)
.then(router.checkRoute) // this is where the route is determined and path parameters are added to the event
.then(authorizer.widget.get)
.then(routes.getWidget)