So I'm setting up a 5 day weather forecast web app for practice interacting with APIs using the MERN stack. I'm using Axios.js to send and respond to requests; to make sure I have my back-end working, I started building that out first before starting to communicate with the API. However, the button I have set up on the front-end (which sends a get request to my server for json data) always returns a response object with response.data having the value of:
RESPONSE: <!doctype html>
<html>
<head>
<meta name="viewport" charset="UTF-8" content="width=device-width, initial-scale=1.0">
</head>
<body>
<div id="app"></div>
<script src="./dist/bundle.js"></script>
</body>
</html>
instead of
RESPONSE: "hello there!"
for a JavaScript that looks like:
{data: "hello there!"}
I know I'm probably missing a step when sending and receiving these requests, but after doing research into this I'm still not sure why I'm not receiving the expected result. My files are set up like this:
-weather_forcast
-client
-src
-components(empty)
app.jsx
-public
-dist
bundle.js
index.html
-server
-routes
routes.js
index.js
package.json
webpack.config.js
The contents of the files that currently have code in them are:
app.jsx
import React, {Component} from 'react';
import ReactDOM, {render} from 'react-dom';
import axios from 'axios';
// import daysOfWeek from './daysOfWeek.jsx';
class App extends Component {
constructor() {
super();
this.state = {
}
this.getData = this.getData.bind(this);
}
getData() {
axios.get('/')
.then((response) => {
console.log("RESPONSE:", response.data);
})
.catch((error) => {
console.log(error);
})
}
render() {
return(
<div>
<button onClick={this.getData}>Hello world</button>
</div>
)
}
}
render(<App/>, document.getElementById('app'));
index.html
<!doctype html>
<html>
<head>
<meta name="viewport" charset="UTF-8" content="width=device-width, initial-scale=1.0">
</head>
<body>
<div id="app"></div>
<script src="./dist/bundle.js"></script>
</body>
</html>
routes.js
let express = require('express');
let router = express.Router();
router.get('/', (req, res) => {
res.send({data:'hello there!'});
});
module.exports = router;
index.js
const express = require('express');
const fs = require('fs');
const path = require('path');
const bodyParser = require('body-parser');
const router = require('./routes/routes.js');
const app = express();
let port = 8000;
app.use(bodyParser.urlencoded());
app.use(bodyParser.json());
app.use(express.static(path.join(__dirname, '../public')));
app.use('/', router);
app.listen(port, () => {
console.log(`express is listening on port ${port}`);
});
webpack.config.js
const path = require('path');
const SRC_DIR = path.join(__dirname, '/client/src');
const DIST_DIR = path.join(__dirname, '/public/dist');
module.exports = {
entry: `${SRC_DIR}/app.jsx`,
output: {
filename: 'bundle.js',
path: DIST_DIR
},
module: {
rules: [
{
test: /\.jsx?/,
include: SRC_DIR,
exclude: /(node_modules|bower_components)/,
use: {
loader: 'babel-loader',
options: {
presets: ['@babel/preset-env', '@babel/preset-react']
}
}
}
]
}
}
The same problem presents itself before I added the "routes" folder and had set up my index.js file like this:
const express = require('express');
const fs = require('fs');
const path = require('path');
const bodyParser = require('body-parser');
const router = require('./routes/routes.js');
const app = express();
let port = 8000;
app.use(bodyParser.urlencoded());
app.use(bodyParser.json());
app.use(express.static(path.join(__dirname, '../public')));
app.get('/', (req, res) => {
res.send({data: "hello there!"});
);
app.listen(port, () => {
console.log(`express is listening on port ${port}`);
});
Any help would be greatly appreciated! I can't seem to get my json object to the front end as data, but I'm not sure what I'm missing from this set up.