Skip to content

Async handler for Express router. Both handle async error and centralize response

Notifications You must be signed in to change notification settings

sonht1109/express-async-handlr

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

21 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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)
  }
}))

About

Async handler for Express router. Both handle async error and centralize response

Resources

Stars

Watchers

Forks

Packages

No packages published