Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/chrisvilches/parallel-express-middleware

Utility that assembles multiple Express middlewares into a new middleware, and executes them in parallel.
https://github.com/chrisvilches/parallel-express-middleware

async-await express expressjs middleware node nodejs npm npm-package parallel

Last synced: 5 days ago
JSON representation

Utility that assembles multiple Express middlewares into a new middleware, and executes them in parallel.

Awesome Lists containing this project

README

        

# Parallel Express Middleware

Utility that assembles multiple Express middlewares into a new middleware, and executes them in parallel.

## Install

```bash
npm install parallel-express-middleware --save
```

## Usage

```javascript
const parallel = require('parallel-express-middleware');

const getUsers = async (req, res, next) => {
res.locals.users = await User.all();
next();
}

const getArticles = async (req, res, next) => {
res.locals.articles = await Article.all();
next();
}

router.get('/', parallel(getUsers, getArticles), (req, res) => {
// Success
res.render('index');
}, (err, req, res, next) => {
// Error happened
res.send('Error!');
});
```

## Warnings

A few things must be kept in mind when using this utility.

1. Rendering functions like `render`, or `json` will be disabled and will throw an error when attempted to be used inside middlewares created using `parallel`.
2. Any exception (runtime error) that occurs inside the middlewares will execute the `next()` using the exception raised as its argument.
3. The resulting middleware generated by `parallel` will execute `next()` with an error argument as soon as any middleware calls `next()` with an error argument.
4. Order of execution is unknown. Only middlewares that don't depend on each other should be used. Don't use middlewares that modify the same data in `res.locals`, or a middleware that needs data created by another middleware being executed in parallel.