-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathREADME.hbs
More file actions
59 lines (43 loc) · 1.8 KB
/
Copy pathREADME.hbs
File metadata and controls
59 lines (43 loc) · 1.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# Lambda Kernel
Kernel to handle and provide structure to requests and responses for AWS Lambda and Serverless.
[](https://img.shields.io/npm/v/lambda-kernel.svg)
This module provides the necessary building blocks for you to build a structured Lambda function.
Initially it is targeting Serverless v1, but easily used for vanilla Lambda.
## Install
```
npm i -S lambda-kernel
```
## Basic Usage
```javascript
// handler.js (the lambda entrypoint)
const Kernel = require('lambda-kernel');
// controller is your own business logic
const controller = require('./lib/controller');
// instantiate the kernel, global configuration
// can be passed into the constructor here
const kernel = new Kernel();
module.exports = {
exampleAction: kernel.dispatch(controller.exampleAction)
};
```
```javascript
// ./lib/controller.js (your business logic)
const Response = require('lambda-kernel/lib/Response');
module.exports = {
exampleAction: (req, env) => {
return new Response(`this is an example in ${env.stage}`, 200);
}
};
```
### Middleware
Middleware provides a clean and re-usable way to instantiate services needed for your actions.
You can create middleware by extending the middleware class and passing your middleware
instances in an array into the Kernel constructor to apply it globally or as a parameter of the dispatch method to
apply it to that action only.
`Middleware#create` is passed the same `req` and `env` parameters as controller methods.
The return value of the `Middleware#create` method will then be provided to the controller method
in the order that the middleware was provided (after the `req`, `env` parameters).
Action-specific middleware is always provided in order after the Kernel middleware.
{{>main}}
* * *
© 2017 Matthew Webb