Socket
Socket
Sign inDemoInstall

aws-simple-lambda-router

Package Overview
Dependencies
3
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    aws-simple-lambda-router

Class to route apigateway requests inside lambda


Version published
Weekly downloads
3
increased by200%
Maintainers
1
Install size
658 kB
Created
Weekly downloads
 

Readme

Source

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)

Keywords

FAQs

Last updated on 07 Sep 2017

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc