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