Bragg: AWS Lambda Web Framework

1.4.0 · active · verified Sun Apr 19

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

Warnings

Install

Imports

Quickstart

This example demonstrates how to initialize Bragg, add a basic middleware to set the response body, configure an error handler, and export the resulting function for AWS Lambda.

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();

view raw JSON →