If my index.js is in the same directory as my index.html, with a css folder containing styles.css like this:
/
|- index.js
|- index.html
|- css/styles.css
In the index.html file, I can reference the css style using a relative path that directs to css/styles.css relative to the index.html file:
<link rel="stylesheet" href="css/styles.css">
However, when my express server's index.js receives a GET request, I need to use an absolute path:
app.get("/", function(req, res) {
res.sendFile(__dirname + "/index.html");
});
Why does the file system behave differently in the case of specifying paths in index.html versus index.js?
The question seeks to understand why an absolute path is required in the index.js file instead of a relative one (and why errors occur).