# async-middleware-stack

> Simple middleware controller. Like express, but async.

Latest version **1.0.1** (published 2018-02-20) · MIT license · 0 weekly downloads

## Install

```sh
npm install async-middleware-stack
pnpm add async-middleware-stack
yarn add async-middleware-stack
bun add async-middleware-stack
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.1 |
| Published | 2018-02-20 |
| First published | 2018-02-20 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 4.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Author | Kaptard |
| Maintainers | kaptard |

## Links

- npm: https://www.npmjs.com/package/async-middleware-stack
- Repository: https://github.com/kaptard/async-middleware-stack
- Homepage: https://github.com/kaptard/async-middleware-stack#readme
- Issues: https://github.com/kaptard/async-middleware-stack/issues
- npm.io page: https://npm.io/package/async-middleware-stack

## Recent versions

- 1.0.1 (latest) — 2018-02-20
- 1.0.0 — 2018-02-20

## README

# async-middleware-stack
Simple middleware controller. Like express, but async.

<br>

## Usage
```js
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`.

<br>

## 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 |

<br>

### 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. | `'*'` |

<br>

### 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 |

<br>

---
_Source: https://npm.io/package/async-middleware-stack · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
