Socket
Book a DemoInstallSign in
Socket

aws-lambda-event-router

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

aws-lambda-event-router

routing inside lambda execution by event

0.0.5
latest
Source
npmnpm
Version published
Weekly downloads
3
-25%
Maintainers
1
Weekly downloads
 
Created
Source

aws-lambda-event-router

URL-like inside routing for AWS lambda.

const router = require("aws-lambda-event-router").createRouter();

router.get("/", (event, context, params) => {
 context.succeed("welcome");
});

router.post("/books/:id", (event, context, params) => {
 context.succeed("this is book " + params.id);
});

exports.handler = router;

usage

Defining a router in lambda execution.

router.get("/", (event, context, params) => {
 context.succeed("welcome");
});

The router requires path and httpMethod in an event object. path should be url-formed string. A minimum event object is:

{
  "path" : "/",
  "httpMethod" : "GET"
}

Finally, exports the router as handler:

exports.handler = router;

Then a response will be welcome.

http methods

Now this library supports following methods.

  • GET
  • POST
  • PUT
  • DELETE
  • HEAD
  • PATCH
  • OPTIONS

To set a callback for a request, router.get, router.post, router.put, router.delete, router.head, router.patch, and router.options are provided.

router.get("/", (event, context, params) => {
  context.succeed("get");
});

router.post("/", (event, context, params) => {
  context.succeed("post");
});

router.put("/", (event, context, params) => {
  context.succeed("put");
});

router.delete("/", (event, context, params) => {
  context.succeed("delete");
});

router.head("/", (event, context, params) => {
  context.succeed("head");
});

router.patch("/", (event, context, params) => {
  context.succeed("patch");
});

router.options("/", (event, context, params) => {
  context.succeed("options");
});

params

/path/:param_name style parameter is supported. You can access a parameter by req.params[param_name]. For instance:

router.get("/books/:id", (event, context, params) => {
  context.succeed("this is book " + params.id);
});
{
  "path" : "/books/1",
  "httpMethod" : "GET"
}

In this case, the response is this is book 1.

use

A router imports sub-routers by use method.

const router = Router.createRouter();
const subRouter = Router.createRouter();
subRouter.get("/hello", (event, context, params) => {
  context.succeed("hello from sub router");
});
router.use("/sub", subRouter);
{
  "path" : "/sub/hello",
  "httpMethod" : "GET"
}

LICENSE

MIT

Keywords

lambda

FAQs

Package last updated on 17 May 2017

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

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.