Below is my code for the /search page on my local webserver. I want to be able to update the page, and display the most recent search query. the problem is that by reloading the page, I end up running the portion that writes to a file (which inadvertently reloads the page) etc etc etc. Leading a vicious cycle of writing and reloading that can quickly get out of hand. Can you help me find a way to avoid this?
app.get('/search/', async (req, res) => {
//console.log(req.body)
// console.log(req.body.query)
var act = null;
var intervalId = setInterval(async function() {
if (req.body.query) {
act = await searchTweets(req.body.query);
} else {
act = await searchTweets("CVE")
}
fs.writeFile("./search.html", `<!DOCTYPE html> <html> <body>\n<a href='http://localhost:${port}/index.html' class='button'>Return to homepage</a>\n` + act + '\n</body>\n</html>', function(err) {
if (err) {
return console.error(err);
}
})
}, 5000);
fs.readFile("./search.html", 'utf8', function(err, data) {
res.send(data);
// Display the file content
console.log(data);
});
});
P.S. to monitor the file for changing I am using livereload:
liverlserver.watch("../BasicServer");