Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

87
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda