I am trying to setup a template site using Node.JS. I am new to using Node.JS and am trying to open up an HTML file and a CSS file before continuing towards the rest of the code. However, even upon reaching reponse.end(), the loop continues to run, and in fact goes for a second time, but does not make it to the end again. I threw in some console.log()s in order to demonstrate this. I will throw both my JS code and mode console output down below. I do not know how to fix this, and any help would be appreciated. Thank you all very much!
const server = require('http').createServer()
const markdown = require( "markdown" ).markdown
const fs = require('fs')
server.on('request', (request, response) => {
response.setHeader('Content-Type', 'text/html')
var page = fs.readFileSync("html/tb.html", 'utf8')
console.log("reading html...")
page+= "<style>"+fs.readFileSync("css/style.css", 'utf8')+"</style>"
console.log("reading css...")
const lang = Intl.DateTimeFormat().resolvedOptions().locale
if (request.url === '/') {
response.end()
}
else if (request.url === '/coucou') {
page+=markdown.toHTML('#Hello world!')
response.end(page)
console.log("end")
}
})
server.listen(4000)
I am sorry for my lack of NodeJS experience, but I could not seem to find an answer to this question anywhere else. Here is my console output when loading ‘coucou’:
reading html...
reading css...
end
reading html...
reading css...
And here it is on the homepage
reading html...
reading css...
reading html...
reading css...
It does not continue after the second round, but the browser never receives an end signal, and therefore continues to load indefinitely. How do I get it to actually process the end of the response and to tell my browser to stop loading?
Again, thank you all very much!!!