AWS λ web framework
This framework is heavily inspired by koa.
$ npm install --save bragg
Adding a single function as middleware is quite easy. The following example will succeed the lambda function with
the value Foo Bar
.
const bragg = require('bragg');
const app = bragg();
app.use(ctx => {
ctx.body = 'Foo Bar';
});
exports.handler = app.listen();
If a promise is assigned to the body
property, it will be resolved before sending the result to the client.
const bragg = require('bragg');
const app = bragg();
app.use(ctx => {
ctx.body = Promise.resolve('Foo Bar');
});
exports.handler = app.listen();
Multiple middlewares will be executed one after the other. The result of the following example is Foo Bar Baz
.
const bragg = require('bragg');
const app = bragg();
app.use(() => {
return 'Foo';
});
app.use((ctx, result) => {
return Promise.resolve(result + ' Bar');
});
app.use((ctx, result) => {
ctx.body = result + ' Baz';
});
exports.handler = app.listen();
In order for you to use parameters provided through API Gateway, you should add a mapping template in the integration request.
#set($path = $input.params().path)
#set($qs = $input.params().querystring)
#set($identity = $context.identity)
{
"identity": {
#foreach($key in $identity.keySet())
"$key": "$util.escapeJavaScript($identity.get($key))"
#if($foreach.hasNext), #end
#end
},
"params": {
#foreach($key in $path.keySet())
"$key": "$path.get($key)"
#if($foreach.hasNext), #end
#end
},
"query": {
#foreach($key in $qs.keySet())
"$key": "$qs.get($key)"
#if($foreach.hasNext), #end
#end
},
"body": $input.json('$')
}
These properties will then be available in the request
object in the middleware function.
- bragg-router - Router middleware.
- bragg-env - Extract the environment.
- bragg-decode-components - Decode the
params
andquery
object. - bragg-safe-guard - Prevents leaking information outside the bragg context.
- bragg-sns - SNS middleware.
- bragg-dynamodb - DynamoDB middleware.
- bragg-cloudwatch - CloudWatch middleware.
- bragg-kms-decrypt - Decrypt properties from the response object.
MIT © Sam Verschueren