Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // async-route.js
- module.exports = handler => async (req, res, next) => {
- try {
- await handler(req, res, next)
- } catch (err) {
- next(err)
- }
- }
- // your-route.js
- exports.sign_in = asyncRoute(async (req, res) => {
- const { body } = req;
- const { email, password } = body;
- const agent = await Agent.findOne({ email: email })
- if (!agent) return res.status(403).send('Not authenticated')
- const isPasswordCorrect = await bcrypt.compare(password, agent.password)
- if (!isPassWordCorrect) return res.status(403).send('Not authenticated')
- ...
- })
- // error-handler.js
- exports.errorHandler = (err, req, res) => {
- // Often you won't send the error message/stack back to the client as it could expose something unwanted
- // Instead you would log it to your own error logging
- const statusCode = err.status || 500
- res.status(statusCode)
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement