I have a react app and a backend of node.js. In my react component i'm fetching some data but the server sends the html page instead. It shows raw html in the screen.
backend(server.js):
app.route("/a").get((req, res)=>{
console.log("hello world");
res.send("hello world")
})
frontend(app.js=>component)
class App extends React.Component{
state = {
res: null
}
componentDidMount(){
fetch("/a")
.then(res=> res.text())
.then(res=>this.setState({
res
}))
}
render(){
return(
<div>
<h1>yay</h1>
<p>{ this.state.res }</p>
</div>
)
}
}
export default App;
(index.js)
import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
import * as serviceWorker from './serviceWorker';
ReactDOM.render(<App />, document.getElementById('root'));
serviceWorker.unregister();
client package.json:
{
"name": "client",
"version": "0.1.0",
"private": true,
"dependencies": {
"react": "^16.8.6",
"react-dom": "^16.8.6",
"react-scripts": "3.0.1"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"proxy": "http://localhost:5000",
"eslintConfig": {
"extends": "react-app"
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
}
}
Yesterday (the day before I posted this question), The app was working fine before I shut off the local servers. But now it acts weird and when I opened the package.json
of client side, I noticed that proxy
property is missing (I don't know if I removed it accidently), I think that I need to mention it assuming that something deleted it.
Thank you in advance.
html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<link rel="shortcut icon" href="%PUBLIC_URL%/favicon.ico" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="theme-color" content="#000000" />
<link rel="manifest" href="%PUBLIC_URL%/manifest.json" />
<title>React App</title>
</head>
<body>
<noscript>You need to enable JavaScript to run this app.</noscript>
<div id="root"></div>
</body>
</html>
hello world
`? – Jamal Abo Jun 25 '19 at 22:33