【发布时间】:2020-02-13 12:52:55
【问题描述】:
我按下提交按钮将数据从'标签'上传到服务器从控制台来了两条消息
路径 enter image description here
错误信息
'加载资源失败:服务器重新调度,状态为404(未找到)
'Uncaght (in phantom) Error: Request with 404.'
Server.js
const express = require("express");
const bodyParser = require("body-parser");
const app = express();
const port = process.env.PORT || 5000;
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
const data = fs.readFileSync("./database.json");
const conf = JSON.parse(data);
const mysql = require("mysql");
const connection = mysql.createConnection({
host: conf.host,
user: conf.user,
password: conf.password,
port: conf.port,
database: conf.database
});
app.get("/api/customers", (req, res) => {
connection.query("SELECT * FROM CUSTOMER", (err, rows, fields) => {
res.send(rows);
});
});
app.listen(port, () => console.log(`Listening on port ${port}`));
App.js
import Customer from "./components/Customer";
import CustomerAdd from "./components/CustomerAdd";
import "./App.css";
import Paper from "@material-ui/core/Paper";
import Table from "@material-ui/core/Table";
import TableHead from "@material-ui/core/TableHead";
import TableBody from "@material-ui/core/TableBody";
import TableRow from "@material-ui/core/TableRow";
import TableCell from "@material-ui/core/Tablecell";
import CircularProgress from "@material-ui/core/CircularProgress";
import { withStyles } from "@material-ui/core/styles";
const styles = theme => ({
root: {
width: "100%",
marginTop: theme.spacing(3),
overflowX: "auto"
},
table: {
minWidth: 1080
}
});
class App extends React.Component {
state = { customers: "", completed: 0 };
componentDidMount() {
this.timer = setInterval(this.progress, 20);
setTimeout(() => {
this.callApi()
.then(res => this.setState({ customers: res }))
.catch(err => console.log(err));
}, 1000);
}
componentWillUnmount() {
clearInterval(this.timer);
}
callApi = async () => {
const response = await fetch("/api/customers");
const body = await response.json();
return body;
};
progress = () => {
const { completed } = this.state;
this.setState({ completed: completed >= 100 ? 0 : completed + 1 });
};
render() {
const { classes } = this.props;
return (
<div>
<Paper className={classes.root}>
<Table className={classes.table}>
<TableHead>
<TableRow>
<TableCell>Number</TableCell>
<TableCell>Image</TableCell>
<TableCell>Name</TableCell>
<TableCell>Birth</TableCell>
<TableCell>Gender</TableCell>
<TableCell>Job</TableCell>
</TableRow>
</TableHead>
<TableBody>
{this.state.customers ? (
this.state.customers.map((c, index) => {
return (
<Customer
id={c.id}
key={index}
image={c.image}
name={c.name}
birthday={c.birthday}
gender={c.gender}
job={c.job}
/>
);
})
) : (
<TableRow>
<TableCell colSpan="6" align="center">
<CircularProgress
className={classes.progress}
variant="determinate"
value={this.state.completed}
/>
</TableCell>
</TableRow>
)}
</TableBody>
</Table>
</Paper>
<CustomerAdd />
</div>
);
}
}
export default withStyles(styles)(App);
CustomerAdd.js
import { post } from "axios";
class CustomerAdd extends React.Component {
constructor(props) {
super(props);
this.state = {
file: null,
userName: "",
birthday: "",
gender: "",
job: "",
fileName: ""
};
this.handleFormSubmit = this.handleFormSubmit.bind(this);
this.handleFileChange = this.handleFileChange.bind(this);
this.handleValueChange = this.handleValueChange.bind(this);
this.addCustomer = this.addCustomer.bind(this);
}
handleFormSubmit(e) {
e.preventDefault();
this.addCustomer().then(response => {
console.log(response.data);
});
}
handleFileChange(e) {
this.setState({
file: e.target.files[0],
fileName: e.target.value
});
}
handleValueChange(e) {
let nextState = {};
nextState[e.target.name] = e.target.value;
this.setState(nextState);
}
addCustomer() {
const url = "/api/customers";
const formData = new FormData();
formData.append("image", this.state.file);
formData.append("name", this.state.userName);
formData.append("birthday", this.state.birthday);
formData.append("gender", this.state.gender);
formData.append("job", this.state.job);
const config = {
headers: {
"content-type": "multipart/form-data"
}
};
return post(url, formData, config);
}
render() {
return (
<form onSubmit={this.handleFormSubmit}>
<h1>고객추가</h1>
프로필 이미지 :{" "}
<input
type="file"
name="file"
file={this.state.file}
value={this.state.fileName}
onChange={this.handleFileChange}
/>{" "}
<br />
이름 :{" "}
<input
type="text"
name="userName"
value={this.state.userName}
onChange={this.handleValueChange}
/>
<br />
생년월일 :{" "}
<input
type="text"
name="birthday"
value={this.state.birthday}
onChange={this.handleValueChange}
/>
<br />
성별 :{" "}
<input
type="text"
name="gender"
value={this.state.gender}
onChange={this.handleValueChange}
/>
<br />
직업 :{" "}
<input
type="text"
name="job"
value={this.state.job}
onChange={this.handleValueChange}
/>
<br />
<button type="submit">추가하기</button>
</form>
);
}
}
export default CustomerAdd;
【问题讨论】:
-
你确定服务器在
3000端口上运行吗? -
没有。服务器在 5000 端口运行。3000 个端口在客户端上运行。
-
知道了,你指向了错误的端口。
-
我在客户端的package.json中设置了5000端口作为代理地址。 "代理":"localhost:5000"
-
你还面临这个问题吗??
标签: javascript node.js reactjs