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

152
Visualizações
Get headers on page load from node-express app?

I am building an app with node and express and set it up such that when the page loads, some headers are sent to the client. How do I extract that information on the client side? I am using JS/jQuery on the client side. Here's the code in question-

app.get('/', (req, res) => {
  models.game_session.build({
    sid: req.sessionID
  }).save().then((data) => {
    let options = {
      "root": __dirname,
      headers: {
        "x-sid": data.id
      }
    }
    res.sendFile('/index.html', options, (err) => {
      if (err) {
        res.send(err)
      }
    });
  }).catch((e) => {
    res.send(`There was an error`);
  });
});

I am saving the session ID in the database then sending the index file to the client. As you see in the options object, I want to include the session ID in the header to be sent with the index file. How can I retrieve that info on the client side? I'm not sure if I am going about this in the right way- appreciate any help!

about 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

Unfortunately there isn't an API to get the headers from you current page, you need to generate another request from you JS/Jquery to get the headers:

How to do that:

How to get read data from response header in jquery/javascript

Accessing the web page's HTTP Headers in JavaScript

about 4 years ago · Santiago Trujillo Relatório

0

EDIT: sorry, didn't read closely enough. It's not actually possible, and here's why:

https://stackoverflow.com/a/220233/4045156

What you are doing is fine, but you could also (once the page loads on the client side) make another request to the server and then respond with a simple JSON object. It's totally up to you though. This of course means another request, but it doesn't seem possible to avoid another request anyways.

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