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

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

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 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