vendredi 31 juillet 2020

How to implement a middleware using object oriented design pattern in node.js?

I am new to an object-oriented design pattern. I just want to convert my middleware function and models to follow an object-oriented design pattern and use that throughout my code.

// sample middleware

const isActivated = async (req, res, next) => {
  const { email } = req.body
  try {
    const user = await User.findOne({ email: email })
    if (!user) {
      next(new Error('No such user is found!'))
    }
    if (user && !user.isActivated) {
      next(new Error('Please activate the account!'))
    }
  } catch (Error) {
    return res.status(HttpStatus.BAD_REQUEST).json({ Error })
  }
}

module.exports = isActivated

Aucun commentaire:

Enregistrer un commentaire