A middleware dispatcher and composer for Node.js applications. It provides a clean way to register and invoke a chain of middleware functions.
npm add @aldojs/middlewareTo 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::dispatchaccepts any input, not only objects.
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
})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
])