@aldojs/middleware
TypeScript icon, indicating that this package has built-in type declarations

1.1.1 • Public • Published

A middleware dispatcher and composer for Node.js applications. It provides a clean way to register and invoke a chain of middleware functions.

Install

npm add @aldojs/middleware

Dispatcher

To create a new Dispatcher instance, you may use createDispatcher utility:

let { createDispatcher } = require('@aldojs/middleware')

let dispatcher = createDispatcher()

Registering middlewares can be done using Dispatcher::use method

dispatcher.use(function middleware () { return 123 })

To dispatch some input data to the middleware chain, and await the result, you may use the Dispatcher::dispatch method

let result = await dispatcher.dispatch({ input: 'foobar' })

Dispatcher::dispatch accepts any input, not only objects.

Middleware

Each middleware could return any output value including promises.

Whether a middleware runs before or after a downstream middlewares depends on the middleware itself. For example, the following middleware would perform some task before the others

dispatcher.use((input, next) => {
  // Perform task

  return next()
})

However, this middleware would perform its task after the input is handled by the following middlewares

dispatcher.use(async (input, next) => {
  let result = await next()

  // Perform the task

  return result
})

Compose

Composing multiple middleware to make a single handler is easy using the compose utility:

const { compose } = require('@aldojs/middleware')

let middleware = compose([
  someMiddleware,
  AnotherMiddleware,
  WhyNotAThirdMiddleware
])

Package Sidebar

Install

npm i @aldojs/middleware

Weekly Downloads

6

Version

1.1.1

License

MIT

Unpacked Size

6.46 kB

Total Files

6

Last publish

Collaborators

  • absolux