1.0.5 • Published 3 years ago

parallel-express-middleware v1.0.5

Weekly downloads
-
License
ISC
Repository
-
Last release
3 years ago

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.
1.0.5

3 years ago

1.0.4

3 years ago

1.0.3

3 years ago

1.0.2

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago