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

0.1.2 • Public • Published

ExpressJS Async Handler

Installation

yarn add express-async-handlr

or

npm i express-async-handlr

Usage

This module can both handle async exception and returning data

First, add these middlewares into your express application

import express from 'express';

const app = express();

// middleware that handles async error
app.use((error, req, res, next) => {
  console.log({ error })
  return res.status(500).json({ message: error?.message });
});

// middleware that handles returning data
app.use((req, res, next) => {
  const data = res.locals.data;
  return res.status(200).json({ data });
});

And then, import express-async-handlr and wrap it onto every async function that needs handing

const asyncHandler = require('express-async-handlr')

app.get('/', asyncHandler(async (req, res, next) => {
  const users = await userRepository.findAll(); // errors will be automatically handled if any
  return users;
}))

Without express-async-handlr, you have to do try/catch like this on every function

app.get('/', asyncHandler(async (req, res, next) => {
  try {
    const users = await userRepository.findAll();
    res.json({ data: users })
  }
  catch(e){
    next(e)
  }
}))

Package Sidebar

Install

npm i express-async-handlr

Weekly Downloads

32

Version

0.1.2

License

ISC

Unpacked Size

3.94 kB

Total Files

5

Last publish

Collaborators

  • sonht1109