Uncaught (in promise) SyntaxError: Unexpected token '<', "<!DOCTYPE "... is not valid JSON
My backend in node.js
and express.js
import express from 'express';
import bcrypt from 'bcrypt-nodejs';
import cors from 'cors';
const app = express();
app.use(express.urlencoded({extended: false}));
app.use(express.json());
app.use(cors());
const database = { users: [
{
id: '123',
name: 'John',
email: 'john@gmail.com',
password: 'cookies',
entries: 0,
joined: new Date()
},
{
id: '124',
name: 'Tom',
email: 'Tom@gmail.com',
password: 'apple',
entries: 0,
joined: new Date()
}
}
app.get('/', (req, res) =>{
res.send(database.users)
})
app.listen(3002, () => {
console.log('app is running on port 3002');
})
My frontend is in React.js
It is a big project so I will only show the part that is causing the error which the response.json()
part. When you get rid of json()
everything is fine but in order for me to recieve data from backend i need to do .json()
which gives that error. Let me know if additional info is needed
componentDidMount(){
fetch('http://localhost:3000')
.then(response => response.json())
.then(console.log)
}