parallel-express-middleware

1.0.5 • Public • Published

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.

  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.

Package Sidebar

Install

npm i parallel-express-middleware

Weekly Downloads

17

Version

1.0.5

License

ISC

Unpacked Size

10.8 kB

Total Files

5

Last publish

Collaborators

  • felovilches