Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

304
Views
cannot set header before they are sent to client error

I am facing error while showing JSON response.

My code is here.

app.get('/api/:id/:uid?',(req,res)=>{//? mean optional parameter
    console.log(req.params);// to get parameter in console
    const id=req.params.id*1;
    console.log(id);
    const tour=tours.find(el=>el.id===id);
    res.status(200).json({
        "staus":"success",
        "tours":tour
    })
    res.send("done");
})

showing Cannot set headers after they are sent to the client

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

I think error message is Cannot set headers after they are sent to the client, not before. It means that you already sent response back to the client. Just remove res.send("done"); this line.

But, be careful about other case like below. You'll also get Cannot set headers after they are sent to the client.

app.get("/path", (req, res) => {
    some_condition = true
    if (some_condition) {
        res.status(200).json({success: true})
    }
    res.status(200).json({success: false})
})

So, you should add return in front of res.status(200).json({success: true}) like below.

app.get("/path", (req, res) => {
    some_condition = true
    if (some_condition) {
        return res.status(200).json({success: true})
    }
    res.status(200).json({success: false})
})

Then, it'll not show up Cannot set headers after they are sent to the client this error message.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!