Basically, I am trying to get data from server side and use it on client side. It is pretty simple but I seem to be missing somehting since it is not working.
Please see below my code on client side on a file called index.html in same folder:
fetch('/lines', { method: 'GET', headers: { 'Content-Type': 'application/json' } }) .then(response => response.json()) .then(linesOfGame => { console.log('SUCCESS') for (const line of linesOfGame) { const lines = document.querySelector('.lines') const newLine = document.createElement('div') newLine.textContent = line lines.appendChild(newLine) } })
Please see below my code on server side on a file called script.js in same folder:
`const express = require(‘express’);
const app = express();
app.listen(3000, () => console.log(‘listening at 3000’));
app.get('/lines', (req, res) => {
console.log('SUCCESS')
const linesOfGame =[' joseph', 'jacob', 'hart']
res.json(linesOfGame);
});
`
I expected this to just work successfully but it seems to not send the array over.
On the server side no error comes up. However, on the console in the browser it says “index.html:30
GET http://127.0.0.1:5500/lines 404 (Not Found)”. Not to sure what to do here. Any help would be great.
joey john is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.