Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

88
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda