parallel-express-middleware
v1.0.5
Published
Utility that assembles multiple Express middlewares into one, and executes them in parallel.
Downloads
131
Maintainers
Readme
Parallel Express Middleware
Utility that assembles multiple Express middlewares into a new middleware, and executes them in parallel.
Install
npm install parallel-express-middleware --save
Usage
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.
- Rendering functions like
render
, orjson
will be disabled and will throw an error when attempted to be used inside middlewares created usingparallel
. - Any exception (runtime error) that occurs inside the middlewares will execute the
next()
using the exception raised as its argument. - The resulting middleware generated by
parallel
will executenext()
with an error argument as soon as any middleware callsnext()
with an error argument. - 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.