Home > Back-end >  How to use asynchandler in class in nodejs
How to use asynchandler in class in nodejs

Time:12-15

I am trying to change my routers into class but I don't know how to wrap the asyncHandler in function inside the class

userController.js

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

class UserController {
    async Register(req, res){
        const {firstName} = req.body
        return res.status(200).send(firstName)
    }
}
const userController = new UserController()
module.exports = userController

userRouter.js

const express = require('express')
const userController = require('../controllers/userController')
const router = express.Router()

router.post('/register', userController.Register)

module.exports = router

CodePudding user response:

You can actually use asyncHandler like this, As per the docs

router.post('/register', asyncHandler(userController.Register))
  • Related