Skip to content

Kaptard/async-middleware-stack

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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(async (req, res) => {
  await new Promise(resolve => setTimeout(resolve, 5000)) // Wait for 5s
  console.log('Praise kek')
})

// Run the stack
const req = { /** usual node.js req object **/ }
const res = { /**           ^^^^           **/ }
const passed = await stack.run(req, res)

if (passed) {
  // Continue if middleware stack passed
}

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:

Argument Description Default
middleware Existing array of functions to base the stack on None

stack.use(route, fn, method)

Adds a function to the middleware stack.

Argument Description Default
route URL to limit the function to. If first arg is a function, it'll be considered as fn, NOT as route. '*'
fn Function to execute with req, res, opitonal objects. None
fn RESTful 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

Argument Description Default
req Request object from node.js/express. Must have url property. None
res Response object from node.js/express. None
optional Optional object to pass to every function. None

About

Simple middleware controller. Like express, but async.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published