Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@middy/http-router

Package Overview
Dependencies
Maintainers
3
Versions
110
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@middy/http-router

HTTP event router for the middy framework

  • 4.0.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
11K
decreased by-29.48%
Maintainers
3
Weekly downloads
 
Created
Source

Middy http-router lambda handler

Middy logo

HTTP router for the middy framework, the stylish Node.js middleware engine for AWS Lambda

npm version npm install size GitHub Actions CI status badge
Standard Code Style Known Vulnerabilities Language grade: JavaScript Core Infrastructure Initiative (CII) Best Practices
Chat on Gitter Ask questions on StackOverflow

You can read the documentation at: https://middy.js.org/docs/routers/http-router

This handler can route to requests to one of a nested hander based on method and path of an http event from API Gateway (REST or HTTP) or Elastic Load Balancer.

Install

To install this middleware you can use NPM:

npm install --save @middy/http-router

Options

  • routes (array[{method, path, handler}]) (required): Array of route objects.
    • method (string) (required): One of GET, POST, PUT, PATCH, DELETE, OPTIONS and ANY that will match to any method passed in
    • path (string) (required): AWS formatted path starting with /. Variable: /{id}/, Wildcard: /{proxy+}
    • handler (function) (required): Any handler(event, context) function

NOTES:

  • Errors should be handled as part of the router middleware stack or the lambdaHandler middleware stack. Handled errors in the later will trigger the after middleware stack of the former.
  • Shared middlewares, connected to the router middleware stack, can only be run before the lambdaHandler middleware stack.

Sample usage

import middy from '@middy/core'
import validatorMiddleware from '@middy/validator'

const getHandler = middy()
  .use(validatorMiddleware({eventSchema: {...} }))
  .handler((event, context) => {
    return {
      statusCode: 200,
      body: '{...}'
    }
  })

const postHandler = middy()
  .use(validatorMiddleware({eventSchema: {...} }))
  .handler((event, context) => {
    return {
      statusCode: 200,
      body: '{...}'
    }
  })

handler = middy()
  .use(httpHeaderNormalizer())
  .handler(httpRouterHandler([
    {
      method: 'GET',
      path: '/user/{id}',
      handler: getHandler
    },
    {
      method: 'POST',
      path: '/user',
      handler: postHandler
    }
  ]))
  

module.exports = { handler }

Middy documentation and examples

For more documentation and examples, refers to the main Middy monorepo on GitHub or Middy official website.

Contributing

Everyone is very welcome to contribute to this repository. Feel free to raise issues or to submit Pull Requests.

License

Licensed under MIT License. Copyright (c) 2017-2022 Luciano Mammino, will Farrell, and the Middy team.

FOSSA Status

Keywords

FAQs

Package last updated on 25 Nov 2022

Did you know?

Socket

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
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc