My current approach involves creating an empty array, extracting the value associated with the first name key from a separate object, sending it to localhost through a Node.js server, iterating back to access new data from another object and adding it to the array. However, I encounter an error message when attempting to host the server for the second loop, stating "throw error, address already in use at 127.0.0.1:3000"
I am uncertain of alternative methods to continuously provide information to a running server
var p = 0, repeat = 4
var indices = []
function f() {
//example of array information fed into the loop
var mc = [{ name: "Henry", output: -30 }, { name: "Kevin", output: -15 }, { name: "Jeremy", output: -40 }, {name: "Steven", output: 43}]
p++
if (p < repeat) {
setTimeout(f, 100)
}
var open = mc[0].name
indices.push(open)
//the issue arises here during the second loop, as the server is already running from the first loop and encountering the address already in use error
var toserver = JSON.stringify(indices)
const http = require('http');
const hostname = '127.0.0.1';
const port = 3000;
const server = http.createServer(function (req, res) {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.end(toserver);
});
server.listen(port, hostname, function () {
console.log('Server running at http://' + hostname + ':' + port + '/');
});
}
f()
The issue persists with "throw error, address already in use at 127.0.0.1:3000". I am hopeful that someone can guide me on how to consistently update this server. The array's information updates continuously, visible through console.log. However, the challenge lies in updating the server to display the changes on the browser. Thank you for your understanding.