Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

243
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!