Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialDaniel Stein
90 Pointshi I'm getting a Can't set headers after they are sent. trying to send a res.send
as above
1 Answer
Alexander La Bianca
15,959 PointsHi Daniel,
Can you please post your code. In the meantime. Node throws this error when you are currently writing to the response and then try to write a second response right after it. Common places for this to show up is in loops or not correctly returning the request handler.
app.get('/', (req,res)=>{
//This can throw the error - as you are looping over all items and therefore writng multiple headers
someArray.forEach((item)=>{
res.end(item);
});
});
app.get('/', (req,res)=>{
//This will throw the error as well. As you are writing the header twice to fix it. put a return statement infront of the res.status(200).end('HI') in order to not execute the rest of the function.
if(true) {
res.status(200).end("Hi");
}
res.status(400).end("Hi);
});
Again those are 2 common places in code that can throw this error, but if I saw your code I could help you out more.