Bragg: AWS Lambda Web Framework
Bragg is a concise AWS Lambda web framework that provides a Koa.js-inspired middleware pattern for handling serverless HTTP requests. It simplifies the process of building API Gateway-backed Lambda functions by allowing developers to chain middleware functions that operate on a `ctx` (context) object. As of its current stable version, 1.4.0, Bragg maintains a focused scope, emphasizing a clear request-response cycle and robust error handling through its `app.onError()` mechanism, which supports asynchronous cleanup processes. Its release cadence appears stable rather than rapid, indicating a mature project. A key differentiator is its straightforward approach to promise resolution for `ctx.body`, automatically awaiting results before sending the Lambda response. It requires Node.js version 4 or higher.
Common errors
-
ReferenceError: bragg is not defined
cause Attempting to use `bragg` without correctly requiring it, or using ESM `import` in a CommonJS context.fixEnsure `const bragg = require('bragg');` is present at the top of your file. -
TypeError: Cannot read properties of undefined (reading 'params')
cause The API Gateway mapping template is not configured or is incorrect, resulting in an empty or malformed `request` object within the Lambda event.fixVerify that your API Gateway Integration Request has a mapping template configured to pass parameters, query strings, and the body to your Lambda function's event payload, as described in the Bragg documentation. -
Function.handler is not a function
cause The AWS Lambda handler is not correctly configured to export the function returned by `app.listen()`.fixEnsure your Lambda function's entry point (`index.js` or similar) contains `exports.handler = app.listen();`. -
TypeError: app.listen is not a function
cause `app` variable was not correctly initialized by calling `bragg()` or was overwritten.fixEnsure `const app = bragg();` is called before attempting to use `app.listen()`.
Warnings
- breaking In version 1.4.0, the `onError` function was updated to allow throwing errors directly, which will propagate them to Lambda's default error handling. In previous versions, custom error handling in `onError` might have suppressed these errors unless explicitly re-thrown or handled.
- gotcha For Bragg to receive path parameters, query strings, and body data from API Gateway, you must configure a 'Mapping Template' in the Integration Request section of your API Gateway method. Without this, `ctx.request.params`, `ctx.request.query`, and `ctx.request.body` will be undefined or empty.
- gotcha Bragg relies on CommonJS `require()` syntax and is built for Node.js environments `>=4`. While modern Node.js supports ESM `import` statements, attempting to `import bragg from 'bragg'` directly in an ESM module context without proper transpilation or configuration can lead to runtime errors.
Install
-
npm install bragg -
yarn add bragg -
pnpm add bragg
Imports
- bragg
import bragg from 'bragg';
const bragg = require('bragg'); - app.listen()
exports.handler = app;
exports.handler = app.listen();
- app.use()
app.use(async ctx => { /* ... */ });
Quickstart
const bragg = require('bragg');
const app = bragg();
// Add a simple middleware that sets the response body
app.use(ctx => {
ctx.body = 'Hello, Lambda!';
});
// Add an error handler for unhandled exceptions within middlewares
app.onError(err => {
console.error('An error occurred:', err.message);
// Perform async cleanup or custom error response
return { statusCode: 500, body: 'Internal Server Error' };
});
// Export the handler function for AWS Lambda
exports.handler = app.listen();
// To test locally, you can invoke the handler directly (for demonstration):
// async function testLocal() {
// const event = {}; // Minimal event for basic test
// const context = {};
// const callback = (error, result) => {
// if (error) console.error('Local Test Error:', error);
// else console.log('Local Test Result:', result);
// };
// await exports.handler(event, context, callback);
// }
// testLocal();