1.0.1 • Published 6 years ago

async-middleware-stack v1.0.1

Weekly downloads
16
License
MIT
Repository
github
Last release
6 years ago

async-middleware-stack

Simple middleware controller. Like express, but async.

Usage

const Stack = require('async-middleware-stack')
const stack = new Stack()

// Add middleware function to the stack
stack.use((req, res) => {
  return new Promise(resolve => {
    setTimeout(resolve, 5000)
  })
})

// Run the stack
const req = { /** usual node.js req object **/ }
const res = { /**           ^^^^           **/ }
try {
  await stack.run(req, res)
} catch (err) {
  // middleware chain broken, so you prolly don't wanna proceed.
}

To break the stack chain, simply return any truthy value. stack.run will then return false.

API

new Stack(config)

Returns a Stack controller.

(optional) config options:

ArgumentDescriptionDefault
middlewareExisting array of functions to base the stack onNone

stack.use(route, fn, method)

Adds a function to the middleware stack.

ArgumentDescriptionDefault
routeURL to limit the function to. If first arg is a function, it'll be considered as fn, NOT as route.'*'
fnFunction to execute with req, res, opitonal objects.None
fnRESTful method to limit the function to. Default accepts any.'*'

stack.run(req, res, optional)

Returns a promise resolving with true when all functions are done running

ArgumentDescriptionDefault
reqRequest object from node.js/express. Must have url property.None
resResponse object from node.js/express.None
optionalOptional object to pass to every function.None