Reputation: 4402
I have a MEAN stack based web application and I have a middle ware which validates the request for some authorization purpose. I have written the security checking part but if the user is unauthorized, the express middle ware should stop execution and return back user 401 status with a message. How can I achieve it? I tried to to use return statement in middle ware but it is not working. Though some of them works, it throws an exception and breaks the server. I want just an abrupt ending to the request. Any help will be appreciated.
Upvotes: 0
Views: 2478
Reputation: 991
Assuming that isValid
is a hypothetical function to check the validity of the request.
function authMiddleware(req,res,next){
if(isValid(req)){
next(); // calls the next middleware
}else {
return res.sendStatus(401); // sends HTTP 401 back
}
}
Upvotes: 2