Socket
Socket
Sign inDemoInstall

@middy/http-router

Package Overview
Dependencies
1
Maintainers
3
Versions
100
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    @middy/http-router

http event router for the middy framework


Version published
Weekly downloads
12K
increased by8.15%
Maintainers
3
Install size
33.9 kB
Created
Weekly downloads
 

Readme

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 Known Vulnerabilities Standard Code Style Chat on Gitter

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 baseHandler 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 baseHandler middleware stack.

Sample usage

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

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

const postHandler = middy()
  .use(validatorMiddleware({inputSchema: {...} }))
  .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

Last updated on 13 Jan 2022

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

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc