I am trying to fetch a file called files.json from the root directory of the (locally hosted) NodeJS backend and then print it out in the console.
<script>
fetch("./files.json")
.then(res => {
return res.json();
})
.then(obj => {
console.log(obj);
})
.catch(err => {
console.error(err);
});
</script>
Using this code in the index.ejs file delivers the following error in the browser's console:
GET http://localhost:3000/files.json [HTTP/1.1 404 Not Found 2ms]
SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data
However, when I use the exact same code in an .html file that's hosted locally with Visual Studio Code's LiveServer plugin, I get the desired result:
Object { files: (3) […] }
If it's relevant, files.json has the following content:
{
"files": [
{
"name": "testfile1"
},
{
"name": "testfile2"
},
{
"name": "testfile3"
}
]
}