Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2020
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // async-route.js
  2. module.exports = handler => async (req, res, next) => {
  3.   try {
  4.     await handler(req, res, next)
  5.   } catch (err) {
  6.     next(err)
  7.   }
  8. }
  9.  
  10. // your-route.js
  11. exports.sign_in = asyncRoute(async (req, res) => {
  12.   const { body } = req;
  13.   const { email, password } = body;
  14.  
  15.   const agent = await Agent.findOne({ email: email })
  16.   if (!agent) return res.status(403).send('Not authenticated')
  17.  
  18.   const isPasswordCorrect = await bcrypt.compare(password, agent.password)
  19.   if (!isPassWordCorrect) return res.status(403).send('Not authenticated')
  20.  
  21.   ...
  22. })
  23.  
  24. // error-handler.js
  25. exports.errorHandler = (err, req, res) => {
  26.   // Often you won't send the error message/stack back to the client as it could expose something unwanted
  27.   // Instead you would log it to your own error logging
  28.   const statusCode = err.status || 500
  29.   res.status(statusCode)
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement