How to send error http response in express/node js?
Solution 1:
In Node with ExpressJS you can use res.status()
to send the error:
return res.status(400).send({
message: 'This is an error!'
});
In Angular you can catch it in the promise response:
$http.post('/api/therapist-login', data)
.then(
function(response) {
// success callback
console.log("posted successfully");
$scope.message = "Login succesful";
},
function(response) {
// failure callback,handle error here
// response.data.message will be "This is an error!"
console.log(response.data.message);
$scope.message = response.data.message
}
);
Solution 2:
Or use instance of Error
class
response.status(code).send(new Error('description'));