Skip to content

Express.js Error: "Cannot set headers after they are sent to the client" #2015

Answered by TatyOko28
Mercure28 asked this question in Q&A
Discussion options

You must be logged in to vote

This happens when you try to send multiple responses in a single request.

Incorrect:

app.get("/test", (req, res) => {
    res.send("Hello");
    res.send("World");  // Error: Headers already sent
});

Correct: Return after sending a response

app.get("/test", (req, res) => {
    res.send("Hello");
    return;
});

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by Mercure28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants