0.1.2 • Published 1 month ago

express-async-handlr v0.1.2

Weekly downloads
-
License
ISC
Repository
github
Last release
1 month ago

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

1 month ago

0.1.1

1 month ago

0.1.0

1 month ago

0.0.5

1 month ago

0.0.4

1 month ago

0.0.2

1 month ago

0.0.1

1 month ago