Get unlimited public & private packages + package-based permissions with npm Pro.Get started »


0.0.3 • Public • Published

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



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: [
  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 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

.then(router.checkRoute) // this is where the route is determined and path parameters are added to the event


npm i aws-simple-lambda-router

DownloadsWeekly Downloads






Last publish


  • avatar