Friday 21 December 2018

Express.js error handling and responses statuts

Hello guys, i'm making a restful API using Express.js and mongoDB here my code for the login post routeauthRoutes.post("/login", function(req, res, next) { passport.authenticate("local", (err, user, info) => { if (err) { return res.status(400).json(err.message); } if (!user) { res.status(400).json("Wrong email or password!"); } req.logIn(user, err => { if (err) { res.status(500).json(err.message); } res.status(201).json(user); }); })(req, res, next); }); What i want to know its better to use try and catch and put all the code within and return 400 code for example or checking every statement and return a response with an statuts code? like that? i'm really confused about this! can anyone suggest me to right way to deal with this? it's Restful api.Thank you.​​

Submitted December 21, 2018 at 11:15AM by ayech0x2

No comments:

Post a Comment