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

89
Views
Can't set headers

I have some basic authentication in a route that when used is throwing a console error.

Error: Can't set headers after they are sent.
    at ServerResponse.OutgoingMessage.setHeader (_http_outgoing.js:356:11)
    at ServerResponse.header

It only occurs when the "if" statement is true (the code within the if statement runs). When that doesn't run, I am not getting any error and the "home" view renders without error.

routes.get('/scan', (req, res, next) => {
    const orderID = req.query.order;
    const token   = req.query.token;

    if (!hasAccess(token))
        res.status(401).send('Unauthorized');

    res.render('home', {order});
});
about 4 years ago · Santiago Trujillo
2 answers
Answer question

0

This error is thrown when you try to respond more than one time to your request.

To avoid this kind of error, you should return when sending a response, so the function will not continue.

In your case :

routes.get('/scan',  (req, res, next) => {
  const orderID = req.query.order;
  const token   = req.query.token;

  if( !hasAccess( token ) )
    return res.status(401).send('Unauthorized');
  return res.render('home', {order});
});
about 4 years ago · Santiago Trujillo Report

0

You should add a return; after your res.status(401).send('Unauthorized'); to avoid sending a duplicate response.

about 4 years ago · Santiago Trujillo 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!