composer

An efficient middleware composer for Koa.

Source:

Methods

(inner) compose(…input) → {function}

Source:

Compose a method that will efficiently execute multiple middleware for Koa. This makes use of the reusify module to avoid allocating excess objects and functions during runtime when at all possible. As a result, memory churn should be reduced while V8 should be able to properly optimize these methods.

This can be used as a drop-in replacement for koa-compose.

FilterableMiddleware objects can be supplied, describing middleware that should be filtered to only run on specific paths.

Example
app.use(compose(cors(), cache(), router.middleware()));
Parameters:
Name Type Attributes Description
input function | Array.<function()> | composer~FilterableMiddleware | Array.<FilterableMiddleware> <repeatable>

The various middleware to compose together. This can consist of functions or arrays of functions. The input may also be comprised of objects that describe middleware that should be filtered to only run on specific paths.

Returns:

The composed middleware

Type
function

Type Definitions

FilterableMiddleware

Source:
Properties:
Name Type Attributes Default Description
fn function

Koa Middleware

filtered Boolean <optional>
false

Whether or not this middleware should actually be filtered.

rich Boolean <optional>
false

Whether the filter for this middleware is a function or a basic string for comparison.

test String | RegExp | Object <optional>

Required when filtered is true. Either a string for a path.startsWith(...) comparison or an object with a test method that accepts the current path. Conveniently, compiled regular expressions have just such a method.

Filterable Middleware

Type:
  • Object
Example
{
    fn: (ctx, next) => ctx.user ? next() : ctx.throw(401),
    filtered: true,
    rich: false,
    test: "/user"
}