I have a health check route for my application which we use to check the application's health, it's a GET route but we send HTTP HEAD call to the same route but I am getting ERR_STREAM_WRITE_AFTER_END error. When digging into that I found out whenever I try to send response data for type HEAD call this error comes but couldn't find the actual reason for the same. Can someone explain why it's behaving this way? The sample code is given below.
Code with error, it's giving error even with res.json and res.send
router.get('/health', function (req, res) {
res.send({
message: "working great!!!"
});
res.end();
});
Update 1 -> After removing res.send, it's working fine, but still interested to know why by using res.send, it's giving the error. in HTTP HEAD call, client is working as expected it's ignoring response body but why on sever side, we are getting this error?
Remove the res.end() it might solve the issue
And you can try this
router.route("/health").get(function (req, res) {
res.send({
message: "working great!!!"
});
});