Answer the question
In order to leave comments, you need to log in
How to make single point error handling in expressjs application?
There is a simple express application built on the principles of mvc, here is a piece of code from the router
const router = require('express').Router();
const Auth = require('./controllers/AuthController');
router.post('/register', Auth.register);
router.post('/login', Auth.login);
router.post('/current-user', Auth.getCurrentUser);
router.post('/forgot-password', Auth.forgotPassword);
router.post('/reset-password', Auth.resetPassword);
module.exports = router;
register: async (req, res) => {
validator.checkEmail(req.body.email);
if (!validator.errorLength)
await validator.checkEmailNotExist(req.body.email);
validator.checkPassword(req.body.password, req.body.password_confirm || '');
if (validator.errorLength)
return res.sendInputsErrors(validator.getAndCleanErrors());
try {
let user = await User.create({
email: req.body.email,
password: crypt.hash(req.body.password)
});
auth.sendSuccessAuthenticatedResponse(user, req, res);
} catch (e) {
res.sendServerError(e)
}
return true;
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question