Bragg Router Middleware

raw JSON →
2.1.0 verified Thu Apr 23 auth: no javascript maintenance

Bragg Router is a routing middleware specifically crafted for the `bragg` AWS Lambda web framework. It empowers developers to define HTTP routes for serverless functions using a familiar Koa-inspired interface. Routes can handle various HTTP methods (GET, POST, PUT, DELETE, PATCH, HEAD, UPDATE) and support URL parameter matching based on the `matcher` library. A key feature is the ability to chain multiple middleware functions for a single route, processing them sequentially and resolving promises between steps. The current stable version is 2.1.0, released in 2020. As a specialized router for the `bragg` framework, it differentiates itself by providing a concise and functional API tailored for AWS Lambda environments, in contrast to broader Node.js web frameworks. However, the `bragg` ecosystem and `bragg-router` itself appear to be in an unmaintained state, with no new feature development or consistent release cadence since 2021.

error TypeError: bragg_router_1 is not a function
cause Attempting to use an ES module `import` statement for `bragg-router` and then trying to invoke the imported module directly, which is not how the CommonJS export works.
fix
Change the import statement to CommonJS require(): const router = require('bragg-router')();
error Error: app.use() requires a middleware function but got a [object Undefined]
cause The `router.routes` property was passed directly to `app.use()` without invoking it, meaning `app.use()` received `undefined` instead of a function.
fix
Ensure router.routes() is called to return the middleware function: app.use(router.routes());
gotcha This package is written exclusively in CommonJS (CJS) and does not provide ES module (ESM) exports. Direct `import` statements using ESM syntax will fail.
fix For module import, use the CommonJS `require()` syntax: `const router = require('bragg-router')();`
deprecated The `bragg` framework and its associated middleware, including `bragg-router`, appear to be unmaintained. The last significant activity on the `bragg` GitHub repository was in 2021, and `bragg-router` in 2020. This implies no active development, bug fixes, or updates for newer Node.js versions or security vulnerabilities.
fix Evaluate long-term suitability for new projects. Consider actively maintained alternatives if continuous updates, modern features, or security patching are critical. For existing projects, pin exact versions and conduct thorough security audits.
npm install bragg-router
yarn add bragg-router
pnpm add bragg-router

This quickstart demonstrates how to initialize `bragg-router`, define simple and parameterized GET routes, implement a route with multiple asynchronous handlers, and integrate the router's middleware into a `bragg` application for AWS Lambda.

const app = require('bragg')();
const router = require('bragg-router')();

// Define a simple GET route for the root path
router.get('/', ctx => {
    ctx.body = 'Hello from Bragg Router!';
});

// Define a GET route with a URL parameter
router.get('/user/{id}', ctx => {
    ctx.body = `Retrieve user with ID: ${ctx.request.params.id}`;
});

// Define a route with multiple asynchronous handlers
router.get('/chain', 
  () => Promise.resolve('First part'),
  (ctx, result) => {
    ctx.body = `${result} and Second part`;
  }
);

// Attach the router middleware to the bragg application
app.use(router.routes());

// Export the handler for AWS Lambda
exports.handler = app.listen();