I am to Node JS decided to create a simple routing where the file is read based on the url.
My problem is that I don’t quite understand how to use response.end
correctly.
This is my code without using response.end
http
.createServer(function (req, res) {
if (req.url === "/students") {
fs.readFile("./templates/students.html", function (err, data) {
if (!err) {
res.write(data);
}
});
} else {
res.write('Page not found.')
}
})
.listen(3000);
When I apply response.end inside a file read, then the else
condition with an empty url stops working (as expected)
Otherwise, only the else condition works, and reading the file does not work when added at the end of url /students
This has always been a problem for me, I don’t understand how to use response.end
correctly. It’s always confused me. How can I solve this problem?