I am very lost on how I can send data from server to client. I need the client javascript to access the array in the server, but I have no idea how I can do that.
index.js
var http = require('http')
var fs = require('fs')
let avails = [0, 0, 0];
var url = require('url');
var path = require('path');
var express = require('express');
const app = express();
http.createServer(function (req, res) {
let route = req.url.replace('/', '')
let request_url = route === '' || route === '/' ? 'website.html' : route
var room = url.parse(req.url, true).query['r'];
var status = url.parse(req.url, true).query['f'];
avails[room] = parseInt(status);
console.log(avails);
console.log(request_url)
fs.exists(request_url, (exist) => {
if (Boolean(exist) === false) {
res.writeHead(404, {'content-type': 'text/html'})
res.end('Page Not Found')
return null
}
fs.readFile(request_url, function (err, data) {
res.writeHead(200)
res.write(data)
res.end()
})
})
}).listen(8080);
app.post('/data', async (request, response) => {
const json = JSON.stringify(avails);
response.json(json);
});
web.html
<!DOCTYPE HTML>
<html>
<head>
<title>Room Availability Detection System</title>
<link rel="stylesheet" href="website.css">
</head>
<body>
<div class="island">
<div class="title">
<p >Piano Room Availability</p>
</div>
<table id="avail">
<tr id="rooms">
<td>Room 1</td>
<td>Room 2</td>
<td>Room 3</td>
</tr>
<tr id="status">
<td id="1">Available</td>
<td id="2">Available</td>
<td id="3">Available</td>
</tr>
</table>
</div>
<script src="./website.js"></script>
</body>
If someone can guide me how I can do this, I would greatly appreciate it. Thanks in advance.