npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

lambda-router20

v0.1.3

Published

aws lambda router

Downloads

3

Readme

Build Status

lambda router20

This package is web application framework for AWS lambda and API Gateway.

Sample application is here. https://github.com/isamu/aws_sam_sample

install

npm install --save lambda-router20

sample

src/app.js

const lambdaRouter = require ("lambda-router20");

const test = (event, context, callback) => {
  const response = {
    'statusCode': 200,
    'body': JSON.stringify({
      message: 'test',
    }),
  };
  return callback(null, response)
};

const hello = (event, context, callback) => {
  const response = {
    'statusCode': 200,
    'body': JSON.stringify({
      message: event.params.message,
    }),
  };
  return callback(null, response)
}

const errorHandler = (code) => {
  return (event) => {
    return {
      'statusCode': code,
      'body': JSON.stringify({
        message: `${code} error`,
      }),
    }
  };
};

lambdaRouter.setInit((event, context) => {
  // some init function
});

lambdaRouter.setRoutes([
  {method: "GET", path: "test", func: test},
  {method: "POST", path: "hello/:message", func: hello }, 
]);

lambdaRouter.setResponseHandlers({
  400: errorHandler(400),
  401: errorHandler(401),
  404: errorHandler(404),
})

lambdaRouter.setErrorCallback((event, error) => {
  console.log(error);
}); 

const router = lambdaRouter.router
  
module.exports = {
  router,
}

template.yaml for SAM

Resources:
    AWSFunction:
        Type: AWS::Serverless::Function
        Properties:
            Handler: app.router
            CodeUri: src/
            Runtime: nodejs8.10
            Events:
                Topic:
                    Type: Api
                    Properties:
                        Path: /1.0/{proxy+}
                        Method: any

test

$curl -i http://localhost:3000/1.0/test
HTTP/1.0 200 OK
Content-Type: application/json
Content-Length: 18
Server: Werkzeug/0.14.1 Python/3.6.2
Date: Wed, 03 Oct 2018 18:56:45 GMT

{"message":"test"}
$curl -i  -X POST  http://localhost:3000/1.0/hello/my_message
HTTP/1.0 200 OK
Content-Type: application/json
Content-Length: 24
Server: Werkzeug/0.14.1 Python/3.6.2
Date: Wed, 03 Oct 2018 18:56:58 GMT

{"message":"my_message"} 
$ curl -i http://localhost:3000/1.0/not_found
HTTP/1.0 404 NOT FOUND
Content-Type: application/json
Content-Length: 23
Server: Werkzeug/0.14.1 Python/3.6.2
Date: Wed, 03 Oct 2018 18:57:29 GMT

{"message":"404 error"}

session sample

src/app.js

const session = require('lambda-router20/lib/session');

const lambdaRouter = require ("lambda-router20");


lambdaRouter.session.setSessionSecret("SET_SECRET_HERE");

const getSession = (event, context, callback) => {
  const sess = session.getSession(event.headers);
  
  const response = {
    'statusCode': 200,
    'body': JSON.stringify({
      session: sess.valid ? sess.data : null,
    }),
  };
  return callback(null, response)
};

const setSession = (event, context, callback) => {
  const options = session.getSessionHeader({status: "ok"});
  
  const response = {
    'statusCode': 200,
    'body': JSON.stringify({
      message: 'test',
    }),
    'headers': options.headers
  };
  return callback(null, response)
};

lambdaRouter.setRoutes([
  {method: "GET", path: "test/setSession", func: setSession},
  {method: "GET", path: "test/getSession", func: getSession},
]);

const router = lambdaRouter.router
  
module.exports = {
  router,
}

test

set cookie

$ curl -i http://localhost:3000/1.0/test/setSession
HTTP/1.0 200 OK
Set-Cookie: SID=%7B%22status%22%3A%22ok%22%7D.eTkTgF7CmPnvB9WX2f9JLba5qgXjVf969xI6KdXCVmY; Path=/; Expires=Thu, 04 Oct 2018 07:20:09 GMT
Content-Type: application/json
Content-Length: 18
Server: Werkzeug/0.14.1 Python/3.6.2
Date: Wed, 03 Oct 2018 22:34:33 GMT

{"message":"test"}

get cookie

$ curl -i -H "Cookie: SID=%7B%22status%22%3A%22ok%22%7D.eTkTgF7CmPnvB9WX2f9JLba5qgXjVf969xI6KdXCVmY;" http://localhost:3000/1.0/test/getSession
HTTP/1.0 200 OK
Content-Type: application/json
Content-Length: 27
Server: Werkzeug/0.14.1 Python/3.6.2
Date: Wed, 03 Oct 2018 22:35:19 GMT

{"session":{"status":"ok"}}