hagopj13 / node-express-boilerplate

A boilerplate for building production-ready RESTful APIs using Node.js, Express, and Mongoose
MIT License
7k stars 2.05k forks source link

is there a way to get rid of catchAsync function in utils? #137

Closed Mohammadreza99A closed 3 years ago

Mohammadreza99A commented 3 years ago

I want to know if there is a way to get rid of catchAsync function in utils? At the moment, this function wraps the controller methodes.

peter-palmer commented 3 years ago

Hey there. Yes, this can be done with an npm package that is called express-async-errors. You can check it out here: https://www.npmjs.com/package/express-async-errors

npm i express-async-errors

And then you will have to add it in your app.js file like this:

const express = require('express');
require('express-async-errors');
const User = require('./models/user');
const app = express();

app.get('/users', async (req, res) => {
  const users = await User.findAll();
  res.send(users);
});