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

205
Visualizações
Node/Express I can't get the header set up in my route

I have a backend that uses Node and the Express framework and a frontend in React. The code is in Typescript.

My backend uses the elastic search client to fetch the data but I don't think that's where my problem lies.

I need to retrieve a parameter that I fetch from the backend via the http header of my response in my frontend.

To do this I used these two functions from the Express doc http://expressjs.com/en/4x/api.html#res.set and http://expressjs.com/en/4x/api.html#res.json

So I wrote the following code for my route:

searchRouter.get('/search', (asyncHandler(async (req, res) => {
  const response = await client.search<HitResult>({
    index: my-index,
    scroll: "1m",
    body: {
      My elastic request
    }
  });
  res.set('data_to_send',response.body.data_to_send);

  console.log('is there a hearder? ', res.headersSent) //always return false
  
  res.json(response);
})));

But the header does not change with this method. So I tried to create an asynchronous function to fill my header with the following code:

async function buildHeader(response: express.Response<any, Record<string, any>>, headerName: string, headerValue: string | undefined): Promise<any> {
  response.header(headerName,headerValue);
}

searchRouter.get('/search', (asyncHandler(async (req, res) => {
  const response = await client.search<HitResult>({
    index: my-index,
    scroll: "1m",
    body: {
      My elastic request
    }
  });
  await buildHeader(res, 'data_to_send', response.body.data_to_send);

  console.log('is there a hearder? ', res.headersSent) //always return false

  res.json(response);
})));

Whatever I do the header does not change and keeps its basic form, the following one:

content-length: "50643"
content-type: "application/json; charset=utf-8"

Does anyone see what I'm doing wrong and could help me, that would save me. Thanks in advance :)

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

As Itai pointed out to me, my problem came from the middleware I was using, CORS. By adding in the options of this middleware the two following fields:

'allowedHeaders': ['data_to_send'],
'exposedHeaders': ['data_to_send'],

Everything works as expected. For more info: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Access-Control-Allow-Headers

over 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