Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
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.
- Host: GitHub
- URL: https://github.com/chrisvilches/parallel-express-middleware
- Owner: ChrisVilches
- Created: 2021-06-27T15:44:56.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2021-06-27T17:06:34.000Z (over 3 years ago)
- Last Synced: 2025-01-03T19:39:41.634Z (about 1 month ago)
- Topics: async-await, express, expressjs, middleware, node, nodejs, npm, npm-package, parallel
- Language: JavaScript
- Homepage: https://www.npmjs.com/package/parallel-express-middleware
- Size: 34.2 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
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.