GithubHelp home page GithubHelp logo

marcosomma / aws-lambda-router Goto Github PK

View Code? Open in Web Editor NEW

This project forked from spring-media/aws-lambda-router

0.0 2.0 0.0 39 KB

Improved routing for AWS Lambda like SNS and ApiGateway

License: Apache License 2.0

JavaScript 100.00%

aws-lambda-router's Introduction

Build Status npm version dependencies

aws-lambda-router

A small library providing routing for AWS ApiGateway Proxy Integrations and SNS...

Install

$ npm install aws-lambda-router

Usage

const router = require('aws-lambda-router');

exports.handler = router.handler(
    // the router-config contains configs for every type of 'processor'
{
    // for handling an http-call from an AWS Apigateway proxyIntegration we provide the following config:
    proxyIntegration: {
        // activate CORS on all http-methods (OPTIONS requests are handled automagically);
        // if set to true, these default headers will be sent on every response:
        // "Access-Control-Allow-Origin" = "'*'"
        // "Access-Control-Allow-Methods" = "'GET,POST,PUT,DELETE,HEAD'"
        // "Access-Control-Allow-Headers" = "'Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-Amz-Security-Token'"
        cors: true,
        routes: [
            {
                // the request-path-pattern to match:
                path: '/graphql',
                // http method to match
                method: 'POST',
                // provide a function to be called with the propriate data
                action: request=>doAnything(request.body)
            },
            {
                // request-path-pattern with a path variable:
                path: '/article/:id',
                method: 'GET',
                // we can use the path param 'id' in the action call:
                action: request=>getSomething(request.paths.id)
            },
            {
                path: '/:id',
                method: 'DELETE',
                action: request=>deleteSomething(request.paths.id)
            }
        ],
        debug: true,
        // custom mapping of thrown errors to http response code error: 
        // the action can throw an object like
        // "throw {reason: 'NotFound', message: 'object id not found'}"
        // the http response then contains the configured value as response code and the message as the body
        errorMapping: {
            'NotFound': 404,
            'ServerError': 500
        }
    },
    // for handling calls initiated from AWS-SNS:
    sns: {
        routes: [
            {
                // a regex to match the content of the SNS-Subject:
                subject: /.*/,
                // Attention: the message is JSON-stringified 
                action: sns => service.doSomething(JSON.parse(sns.Message))
            }
        ]
    }
});

local developement

The best is to work with npm link

See here: http://vansande.org/2015/03/20/npm-link/

Release History

  • 0.2.2 proxyIntegration: set correct header values now for CORS
  • 0.2.1 proxyIntegration: CORS in Preflight, status code 400 for invalid body, set more CORS headers as default
  • 0.2.0 Attention: breaking changes for configuration; add SNS event process
  • 0.1.0 make it work now
  • 0.0.1 initial release

aws-lambda-router's People

Contributors

chgohlke avatar ujann avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.