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

233
Visualizações
Can an HTML webpage generated by a Node.js server communicate with the server beyond the initial write without reloading?

I'm trying to get a Node.js webserver to write a webpage to a specific URL on the localhost and use the input from a form within that webpage to read things from an SQL database and update every couple of seconds. Logic dictates that end-to-end communication between a client webpage and a server should be handled through WebSockets. However, common sense dictates that having a client webpage query the URL it is currently being accessed on is likely not the correct method of handling things. Furthermore, there doesn't seem to be any way for me to pass the results of the SQL query back to the webpage's embedded script. Is there a method to communicate to and from a webpage and the server that is hosting it without reloading the page every time?

Here's the code that loads the webpage onto the specified localhost port.

var server = http.createServer(function (req, res) {
  var data = fs.readFileSync(__dirname + "/public/index.html");
  res.writeHead(200, { 'Content-Type': 'text/html' });
  res.write(data);
  let id = url.parse(req.url, true).query["id"];
  if (id) console.log(id); // would be replaced with other things
  res.end();
}).listen(port);

Here's the form within the HTML that is meant to initially send the id to the server (reloads the page, but there doesn't seem to be a way to handle this with POSTing the id, only GETting):

<form id="id_form" method="get" autocomplete="on">
  <label for="id">BATMON ID: </label>
  <input type="text" id="id" name="id" autofocus><br>
  <input type="submit">
</form>

**EDIT: ** To clarify, the intended behavior is that the page loads normally, then when I input an ID number into the form the webpage can send it to the server. When the server receives the ID, it should begin periodically using that ID in an SQL query (I'm using ps) and sending the results of that SQL query back to the webpage.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Figured it out, you can use WebSockets to query the server which is hosting the webpage you're using. It may seem somewhat odd, but it's the simplest way to do this.

about 4 years ago · Juan Pablo Isaza 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