我创建了一个简单的React + node-postgres + expressJS + PostgreSQL DB webapp。
它只有两个输入和一个按钮。当我按下按钮时,值将存储在数据库中。
ExpressJS服务器正在使用nodemon运行。如果服务器运行,该应用程序运行正常。当我输入第一个值并按下按钮时,所有内容都会正确存储-短时间后,nodemon服务器崩溃,并显示以下错误。
C:\node tutorial\reactPSQLexpress\backend>nodemon server.js
[nodemon] 1.19.1
[nodemon] to restart at any time, enter `rs`
[nodemon] watching: *.*
[nodemon] starting `node server.js`
Listening to PORT: 7777
{ country_name: 'test', continent_name: 'test' }
Data INSERT succesfull
events.js:167
throw er; // Unhandled 'error' event
^
Error [ERR_STREAM_WRITE_AFTER_END]: write after end
at writeAfterEnd (_stream_writable.js:243:12)
at Socket.Writable.write (_stream_writable.js:291:5)
at Connection.end (C:\node tutorial\reactPSQLexpress\backend\node_modules\pg\lib\connection.js:339:22)
at Client.end (C:\node tutorial\reactPSQLexpress\backend\node_modules\pg\lib\client.js:525:21)
at Pool._remove (C:\node tutorial\reactPSQLexpress\backend\node_modules\pg-pool\index.js:161:12)
at Timeout.setTimeout (C:\node tutorial\reactPSQLexpress\backend\node_modules\pg-pool\index.js:44:12)
at ontimeout (timers.js:436:11)
at tryOnTimeout (timers.js:300:5)
at listOnTimeout (timers.js:263:5)
at Timer.processTimers (timers.js:223:10)
Emitted 'error' event at:
at Client.idleListener (C:\node tutorial\reactPSQLexpress\backend\node_modules\pg-pool\index.js:78:10)
at Client.emit (events.js:182:13)
at Connection.connectedErrorHandler (C:\node tutorial\reactPSQLexpress\backend\node_modules\pg\lib\client.js:182:10)
at Connection.emit (events.js:182:13)
at Socket.reportStreamError (C:\node tutorial\reactPSQLexpress\backend\node_modules\pg\lib\connection.js:72:10)
at Socket.emit (events.js:182:13)
at writeAfterEnd (_stream_writable.js:245:10)
at Socket.Writable.write (_stream_writable.js:291:5)
at Connection.end (C:\node tutorial\reactPSQLexpress\backend\node_modules\pg\lib\connection.js:339:22)
at Client.end (C:\node tutorial\reactPSQLexpress\backend\node_modules\pg\lib\client.js:525:21)
[nodemon] app crashed - waiting for file changes before starting...
我已经杀死了所有节点服务器,并再次启动了所有内容,但是在插入一个值之后,同样发生了。
taskkill /f /im node.exe
kill命令很好用-但问题仍然存在。
编辑:服务器端代码:
app.use(bodyParser.json());
app.use(
bodyParser.urlencoded({
extended: true
})
);
app.use((req, res, next) => {
res.header("Access-Control-Allow-Origin", "*");
res.header(
"Access-Control-Allow-Headers",
"Origin, X-Requested-With, Content-Type, Accept"
);
next();
});
app.post("/api/new-country", (req, res) => {
console.log(req.body);
let country_name = req.body.country_name;
let continent_name = req.body.continent_name;
pool.connect((err, db, done) => {
if (err) {
return res.status(400).send(err)
} else {
db.query("INSERT INTO country (country_name, continent) VALUES($1, $2)",[country_name, continent_name], (err, table) =>{
// db.query("SELECT * from country", (err, table) => {
done();
if (err) {
return res.status(400).send(err)
} else {
// console.log(table.rows);
console.log("Data INSERT succesfull");
db.end();
res.status(201).send({message: "Data inserted!"})
}
});
}
});
});
app.listen(PORT, () => console.log(`Listening to PORT: ${PORT}`));
我将db.end移到了res.status(201)之后...但是它没有帮助。
答案 0 :(得分:0)
我遇到了同样的错误,结果发现我的.env文件中有错字。 我写了
PORT=2000; #instead of PORT=2000