Hello Guys, How Are You All ? Hope You All Are Fine. Today I am trying to use Express to make Some APIs in NodeJS But I am facing Following error Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client in nodejs. So in This Article I am providing you all possible solution to resolve this error.
So lets Start This Article Without Wasting Your Time.
How Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client Occurs?
I am trying to use Express to make Some APIs in NodeJs But I am facing Following error.
Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
at new NodeError (node:internal/errors:371:5)
at ServerResponse.setHeader (node:_http_outgoing:576:11)
Here is What I am trying.
app.post("/login", (req, res) => {
if (!req.body.username) {
res.status(400).json({
status_code: 0,
error_msg: "Require Params Missing",
});
}
res.status(200).json({
status_code: 1,
data: req.body,
});
});
How To Solve Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client ?
How To Solve Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client ?
To Solve Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client In Another Scenario I am Facing error While Using sendStatus() in My Code. So That Just Use status() Instead of sendStatus(). Just Like This: return res.status(200).json Now, your problem must be solved.
Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
To Solve Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client Here You Need To use return statement to solve this error. Error Is occurs Because Of Your res.status(400).json is executing and then your function is not going to stop. Thats Why You Are Facing this error. So that Just add return statement in your request handler function Just like This: return res.status(400).json Now, Your error Must be solved.
Solution 1: Use return statement
Here You Need To use return statement to solve this error. Error Is occurs Because Of Your res.status(400).json is executing and then your function is not going to stop. Thats Why You Are Facing this error.
When a return statement is used in a function body, the execution of the function is stopped. If specified, a given value is returned to the function caller.
So that Just add return statement in your request handler function Just like This.
app.post("/login", (req, res) => {
if (!req.body.username) {
return res.status(400).json({
status_code: 0,
error_msg: "Require Params Missing",
});
}
res.status(200).json({
status_code: 1,
data: req.body,
});
});
Now, Your error Must be solved.
Solution 2: Use sendStatus() instead of status()
In Another Scenario I am Facing error While Using sendStatus() in My Code.
if (!req.body.username) {
return res.sendStatus(200).json({ // Error ERR_HTTP_HEADERS_SENT
status_code: 0,
error_msg: "Require Params Missing",
});
}
So That Just Use status() Instead of sendStatus(). Just Like This.
if (!req.body.username) {
return res.status(200).json({ // Worked
status_code: 0,
error_msg: "Require Params Missing",
});
}
Now, your problem must be solved.
Summary
It’s all About this issue. Hope all solution helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you?
Also, Read
Thanks for the article. It has helped me a lot
Thanks
Thank you
My pleasure to help You Muba and Thank You For your Valuable Word!
Awesome, the first approach solved my issue. Thank you.
It’s my Pleasure to Help You Tosin. Thank You For Your Valuable words.