I want to make a GET request to a local JSON file with express.
In my server.js I have this
var data = {};
app.get('/src/assets/data.json', (req, res) => {
console.log(res)
res.writeHead(200, {
'Content-type': 'application/json'
});
res.end(JSON.stringify(data));
});
data.json looks like this
[{
"param": "one",
"param": "two",
"param": "three"
}]
Also I made a function for GET request which is called as soon as the DOM is loaded
getData() {
let xhr = new XMLHttpRequest();
xhr.open('GET', '/src/assets/data.json', true);
xhr.onreadystatechange = () => {
if (xhr.readyState === XMLHttpRequest.DONE && xhr.status === 200) {
console.log(xhr)
}
};
xhr.send();
}
I'm getting a response back but it is an empty object. I'm guessing it is because in my server file var data = {};
is empty but I'm not sure what to do with it?