async-express-mw
TypeScript icon, indicating that this package has built-in type declarations

0.1.2 • Public • Published

Async Express MW

Just wraps express middlewares so that you can use async functions and have the error passed onto the next function.

Build Status Coverage Status npm version GitHub license

Similar to other existing packages it simply provides a lightweight wrapper function which passes any error to the express js next function. There were some other packages that needed to transpile, had dependencies or didnt have all features such as allowing array, and error handler so created this. Use it if you want.

Usage

Just wrap middlewares with the async wrap thing

// You can use import / export if you want
const wrap = require('async-express-mw');

// Some middleware function can be wrapped
const wrappedMwThing = wrap(async (req, res, next) => {/* does something magical */});

// Multiple mw can be wrapped by providing an array
const firstAmazingMw = async (req, res, next) => {/* does amazing stuff */}
const otherMw = async (req, res, next) =>  {/* does other stuff */}

// Wrapped maybe to express or router
app.get('/amazing-thing/:id', wrap([firstAmazingMw, otherMw]));

// Allows async error mw just as long as, per usual, the handler has the 4 param sig
const errorMw = async (err, req, res, next) => {/* handles all the errorz */}

app.use('/amazing-thing/:id', wrap(errorMw));

Package Sidebar

Install

npm i async-express-mw

Weekly Downloads

3,307

Version

0.1.2

License

MIT

Unpacked Size

6.92 kB

Total Files

7

Last publish

Collaborators

  • lessonteacher