我正在使用nodemon npm软件包在检测到目录中的文件更改时自动重新启动节点应用程序。 通过使用express和http npm软件包创建服务器。 问题是服务器空闲几分钟后会引发错误错误:读取ECONNRESET 实际错误如下:
events.js:167
throw er; // Unhandled 'error' event
^
Error: read ECONNRESET
at TCP.onStreamRead (internal/stream_base_commons.js:111:27)
Emitted 'error' event at:
at Connection._handleProtocolError (/home/abc/node_modules/mysql/lib/Connection.js:425:8)
at Protocol.emit (events.js:182:13)
at Protocol._delegateError (/home/abc/node_modules/mysql/lib/protocol/Protocol.js:390:10)
at Protocol.handleNetworkError (/home/abc/node_modules/mysql/lib/protocol/Protocol.js:363:10)
at Connection._handleNetworkError (/home/abc/node_modules/mysql/lib/Connection.js:420:18)
at Socket.emit (events.js:182:13)
at emitErrorNT (internal/streams/destroy.js:82:8)
at emitErrorAndCloseNT (internal/streams/destroy.js:50:3)
at process._tickCallback (internal/process/next_tick.js:63:19)
我试图抓住错误,但这不能解决我的问题。
process.on('uncaughtException', function(err) {
console.log("uncaughtException called: \n", err);
})
通过使用express和http创建服务器:
express = require('express');
app = express();
http = require('http');
var httpServer = http.createServer(app);
httpServer.listen(3000, function(req, res) {
console.log('Server running at port 3000);
});
我希望防止服务器发生ECONNRESET错误 有什么解决方案?
答案 0 :(得分:0)
基于抛出的错误,我们可以看到这是一个mysql问题:
Emitted 'error' event at:
at Connection._handleProtocolError (/home/abc/node_modules/mysql/lib/Connection.js:425:8)
我遇到了同样的问题,并且解决该问题的方法是在与数据库建立连接之后,我每隔20分钟添加一次setInterval,执行一次简单查询,例如db.query('SELECT 1;')。 。假设mysql在一段时间闲置后关闭了连接,所以基于此post
“模块开发人员建议使用心跳使连接保持活动状态,例如每隔一段时间调用SELECT 1;”
这是一个快速修复,它对我有用,但是如果您想采用其他方法,我建议您阅读上面的文章,因为还有其他解决方案。