因此,我正在设置一个为期 5 天的天气预报网络应用程序,用于练习使用 MERN 堆栈与 API 进行交互。我正在使用 Axios.js 发送和响应请求;为了确保我的后端正常工作,我在开始与 API 通信之前先开始构建它。但是,我在前端设置的按钮(它向我的服务器发送一个获取 json 数据的请求)总是返回一个带有 response.data 的响应对象,其值为:
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>
代替
RESPONSE: "hello there!"
对于看起来像这样的 JavaScript:
{data: "hello there!"}
我知道在发送和接收这些请求时我可能错过了一个步骤,但是在对此进行研究之后,我仍然不确定为什么我没有收到预期的结果。我的文件是这样设置的:
-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
当前包含代码的文件的内容是:
应用程序.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'));
索引.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>
路由.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']
}
}
}
]
}
}
在我添加“路由”文件夹并像这样设置我的 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.get('/', (req, res) => {
res.send({data: "hello there!"});
);
app.listen(port, () => {
console.log(`express is listening on port ${port}`);
});
任何帮助将不胜感激!我似乎无法将我的 json 对象作为数据发送到前端,但我不确定我在这个设置中缺少什么。