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

128
Vistas
Passing ServerResponse by parameter in Express

I want to set some cookies from the Express server, but I want to do it in a function. I'm not sure about what is internally http ServerResponse, but if I set a header inside the function, that header is not available outside it. E.g, in handler

mySetHeader(res);
// if I check res here there is NO header

function mySetHeader(res) {
   res.setHeader('Set-Cookie', serialize('my-cookie', 'my-cookie-value', { path: '/' }));
}

On the other hand if I return res, it works

res = mySetHeader(res);
// if I check res here there IS a header

function mySetHeader(res) {
   res.setHeader('Set-Cookie', serialize('my-cookie', 'my-cookie-value', { path: '/' }));
   return res;
}

I have looked quickly for some examples where res is passed to a function, but I haven't found much. I wonder if this is an acceptable pattern or if I should do this in another way. Maybe through a middleware or something

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

The following works for me:

function mySetHeader(res) {
  res.setHeader("A", "B");
}
app.use(function(req, res) {
  mySetHeader(res);
  res.end();
})

The header is set as expected.

Perhaps you need to share more of your code.

over 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