nodemon 应用程序在提交表单数据后崩溃
nodemon app crashed after submitting form data
我正在使用 React 开发注册表单并将数据存储在 mysql 数据库中。每次启动 运行 服务器和客户端应用程序时,我只能在表单上单击提交一次。点击后数据存入数据库,但服务器端出现如下错误:
[nodemon] starting `node server.js --port 3001`
running on port 3001
null
events.js:377
throw er; // Unhandled 'error' event
^
Error: Connection lost: The server closed the connection.
at Protocol.end (C:\Users\user\OneDrive\Documents\website\server\node_modules\mysql\lib\protocol\Protocol.js:112:13)
at Socket.<anonymous> (C:\Users\user\OneDrive\Documents\website\server\node_modules\mysql\lib\Connection.js:94:28)
at Socket.<anonymous> (C:\Users\user\OneDrive\Documents\website\server\node_modules\mysql\lib\Connection.js:526:10)
at Socket.emit (events.js:412:35)
at endReadableNT (internal/streams/readable.js:1317:12)
at processTicksAndRejections (internal/process/task_queues.js:82:21)
Emitted 'error' event on Connection instance at:
at Connection._handleProtocolError (C:\Users\user\OneDrive\Documents\website\server\node_modules\mysql\lib\Connection.js:423:8)
at Protocol.emit (events.js:400:28)
at Protocol._delegateError (C:\Users\user\OneDrive\Documents\website\server\node_modules\mysql\lib\protocol\Protocol.js:398:10)
at Protocol.end (C:\Users\user\OneDrive\Documents\websiteserver\node_modules\mysql\lib\protocol\Protocol.js:116:8)
at Socket.<anonymous> (C:\Users\user\OneDrive\Documents\website\server\node_modules\mysql\lib\Connection.js:94:28)
[... lines matching original stack trace ...]
at processTicksAndRejections (internal/process/task_queues.js:82:21) {
fatal: true,
code: 'PROTOCOL_CONNECTION_LOST'
}
[nodemon] app crashed - waiting for file changes before starting...
所以我无法提交另一个表单,除非我重新启动服务器。有人知道怎么解决吗?
我的客户端代码:
import React, {useState} from 'react'
import {
FormWrap,
Container,
Icon,
FormContent,
Form,
FormH1,
FormLabel,
FormInput,
Text,
FormButton,
TextLeft,
Heading,
Subtitle,
TextWrapper,
LogoPage}
from './SignInElements'
import Axios from 'axios'
const SignIn = ({}) => {
const[name, setname] = useState("")
const[cpf, setcpf] = useState("")
const[tel, settel] = useState("")
const[email, setemail] = useState("")
const register = () => {
Axios.post('http://localhost:3001/register/', {
name: name,
cpf: cpf,
tel: tel,
email: email,
}).then((response) => {console.log(response)});
};
return (
<>
<Container>
<FormWrap>
<FormContent>
<TextLeft action="#">
<Icon to="/"> <LogoPage src={require('../../images/Brand.svg').default} /> </Icon>
<TextWrapper>
<Heading>Test</Heading>
<Subtitle>Test</Subtitle>
</TextWrapper>
</TextLeft>
<Form action="#">
<FormLabel htmlFor="for">name</FormLabel>
<FormInput placeholder="Nome Sobrenome" type="text" required onChange={(e) => setname(e.target.value) }/>
<FormLabel htmlFor="for">cpf</FormLabel>
<FormInput placeholder="000.000.000-00" type="number" required onChange={(e) => setcpf(e.target.value) }/>
<FormLabel htmlFor="for">tel</FormLabel>
<FormInput placeholder="(DDD) 90000-0000" type="text" required onChange={(e) => settel(e.target.value) } />
<FormLabel htmlFor="for">email</FormLabel>
<FormInput placeholder="exemplo@email.com" type="email" required onChange={(e) => setemail(e.target.value) } />
<FormButton type="submit" onClick={register}>Register</FormButton>
</Form>
</FormContent>
</FormWrap>
</Container>
</>
)
}
export default SignIn
我的服务器端代码:
const express = require("express");
const mysql = require("mysql");
const app = express();
const cors = require("cors");
app.use(express.json());
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
res.header("Access-Control-Allow-Headers", "x-access-token, Origin, X-Requested-With, Content-Type, Accept");
next();
});
const db = mysql.createConnection({
user: "--",
host: "--",
password: "--",
database: "--"
});
app.post('/register', (req, res) => {
const name = req.body.name;
const cpf = req.body.cpf;
const tel = req.body.tel;
const email = req.body.email;
db.query("INSERT INTO table (name, cpf, tel, email) VALUES (?,?,?,?)",
[name,cpf,tel,email],
(err,result)=> {console.log(err);
}
)
});
app.listen(3001, () => {
console.log("running on port 3001")
});
尝试强制mysql保持连接。这段代码应该可以做到
setInterval(function () {
db.query('SELECT 1');
}, 5000);
我正在使用 React 开发注册表单并将数据存储在 mysql 数据库中。每次启动 运行 服务器和客户端应用程序时,我只能在表单上单击提交一次。点击后数据存入数据库,但服务器端出现如下错误:
[nodemon] starting `node server.js --port 3001`
running on port 3001
null
events.js:377
throw er; // Unhandled 'error' event
^
Error: Connection lost: The server closed the connection.
at Protocol.end (C:\Users\user\OneDrive\Documents\website\server\node_modules\mysql\lib\protocol\Protocol.js:112:13)
at Socket.<anonymous> (C:\Users\user\OneDrive\Documents\website\server\node_modules\mysql\lib\Connection.js:94:28)
at Socket.<anonymous> (C:\Users\user\OneDrive\Documents\website\server\node_modules\mysql\lib\Connection.js:526:10)
at Socket.emit (events.js:412:35)
at endReadableNT (internal/streams/readable.js:1317:12)
at processTicksAndRejections (internal/process/task_queues.js:82:21)
Emitted 'error' event on Connection instance at:
at Connection._handleProtocolError (C:\Users\user\OneDrive\Documents\website\server\node_modules\mysql\lib\Connection.js:423:8)
at Protocol.emit (events.js:400:28)
at Protocol._delegateError (C:\Users\user\OneDrive\Documents\website\server\node_modules\mysql\lib\protocol\Protocol.js:398:10)
at Protocol.end (C:\Users\user\OneDrive\Documents\websiteserver\node_modules\mysql\lib\protocol\Protocol.js:116:8)
at Socket.<anonymous> (C:\Users\user\OneDrive\Documents\website\server\node_modules\mysql\lib\Connection.js:94:28)
[... lines matching original stack trace ...]
at processTicksAndRejections (internal/process/task_queues.js:82:21) {
fatal: true,
code: 'PROTOCOL_CONNECTION_LOST'
}
[nodemon] app crashed - waiting for file changes before starting...
所以我无法提交另一个表单,除非我重新启动服务器。有人知道怎么解决吗?
我的客户端代码:
import React, {useState} from 'react'
import {
FormWrap,
Container,
Icon,
FormContent,
Form,
FormH1,
FormLabel,
FormInput,
Text,
FormButton,
TextLeft,
Heading,
Subtitle,
TextWrapper,
LogoPage}
from './SignInElements'
import Axios from 'axios'
const SignIn = ({}) => {
const[name, setname] = useState("")
const[cpf, setcpf] = useState("")
const[tel, settel] = useState("")
const[email, setemail] = useState("")
const register = () => {
Axios.post('http://localhost:3001/register/', {
name: name,
cpf: cpf,
tel: tel,
email: email,
}).then((response) => {console.log(response)});
};
return (
<>
<Container>
<FormWrap>
<FormContent>
<TextLeft action="#">
<Icon to="/"> <LogoPage src={require('../../images/Brand.svg').default} /> </Icon>
<TextWrapper>
<Heading>Test</Heading>
<Subtitle>Test</Subtitle>
</TextWrapper>
</TextLeft>
<Form action="#">
<FormLabel htmlFor="for">name</FormLabel>
<FormInput placeholder="Nome Sobrenome" type="text" required onChange={(e) => setname(e.target.value) }/>
<FormLabel htmlFor="for">cpf</FormLabel>
<FormInput placeholder="000.000.000-00" type="number" required onChange={(e) => setcpf(e.target.value) }/>
<FormLabel htmlFor="for">tel</FormLabel>
<FormInput placeholder="(DDD) 90000-0000" type="text" required onChange={(e) => settel(e.target.value) } />
<FormLabel htmlFor="for">email</FormLabel>
<FormInput placeholder="exemplo@email.com" type="email" required onChange={(e) => setemail(e.target.value) } />
<FormButton type="submit" onClick={register}>Register</FormButton>
</Form>
</FormContent>
</FormWrap>
</Container>
</>
)
}
export default SignIn
我的服务器端代码:
const express = require("express");
const mysql = require("mysql");
const app = express();
const cors = require("cors");
app.use(express.json());
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
res.header("Access-Control-Allow-Headers", "x-access-token, Origin, X-Requested-With, Content-Type, Accept");
next();
});
const db = mysql.createConnection({
user: "--",
host: "--",
password: "--",
database: "--"
});
app.post('/register', (req, res) => {
const name = req.body.name;
const cpf = req.body.cpf;
const tel = req.body.tel;
const email = req.body.email;
db.query("INSERT INTO table (name, cpf, tel, email) VALUES (?,?,?,?)",
[name,cpf,tel,email],
(err,result)=> {console.log(err);
}
)
});
app.listen(3001, () => {
console.log("running on port 3001")
});
尝试强制mysql保持连接。这段代码应该可以做到
setInterval(function () {
db.query('SELECT 1');
}, 5000);