expressjs 中的节点服务器在一段时间后自行停止工作

时间:2021-01-22 03:28:56

标签: mysql node.js express node-modules

运行 npm start 后,过了一段时间,然后我在终端中收到此错误。我是新来表达 js 和 node 与 mysql 配对,所以我不能真正理解下面的错误,希望这是足够的上下文。该项目是一个简单的数据库表格内容的表格展示,效果很好,我只需要每10-40秒运行一次npm start即可。

 events.js:291
          throw er; // Unhandled 'error' event
          ^

Error: Connection lost: The server closed the connection.
    at Protocol.end (/Volumes/HDD 500GB/* Documents/2 webdev/projects/nodejs/db connection demo/nodeapp/node_modules/mysql/lib/protocol/Protocol.js:112:13)
    at Socket.<anonymous> (/Volumes/HDD 500GB/* Documents/2 webdev/projects/nodejs/db connection demo/nodeapp/node_modules/mysql/lib/Connection.js:94:28)
    at Socket.<anonymous> (/Volumes/HDD 500GB/* Documents/2 webdev/projects/nodejs/db connection demo/nodeapp/node_modules/mysql/lib/Connection.js:526:10)
    at Socket.emit (events.js:326:22)
    at endReadableNT (_stream_readable.js:1226:12)
    at processTicksAndRejections (internal/process/task_queues.js:80:21)
Emitted 'error' event on Connection instance at:
    at Connection._handleProtocolError (/Volumes/HDD 500GB/* Documents/2 webdev/projects/nodejs/db connection demo/nodeapp/node_modules/mysql/lib/Connection.js:423:8)
    at Protocol.emit (events.js:314:20)
    at Protocol._delegateError (/Volumes/HDD 500GB/* Documents/2 webdev/projects/nodejs/db connection demo/nodeapp/node_modules/mysql/lib/protocol/Protocol.js:398:10)
    at Protocol.end (/Volumes/HDD 500GB/* Documents/2 webdev/projects/nodejs/db connection demo/nodeapp/node_modules/mysql/lib/protocol/Protocol.js:116:8)
    at Socket.<anonymous> (/Volumes/HDD 500GB/* Documents/2 webdev/projects/nodejs/db connection demo/nodeapp/node_modules/mysql/lib/Connection.js:94:28)
    [... lines matching original stack trace ...]
    at processTicksAndRejections (internal/process/task_queues.js:80:21) {
  fatal: true,
  code: 'PROTOCOL_CONNECTION_LOST'
}
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! nodeapp@0.0.0 start: `node ./bin/www`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the nodeapp@0.0.0 start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /Users/thodoristhomaidis/.npm/_logs/2021-01-22T03_09_42_711Z-debug.log
thodoristhomaidis@Theos-iMac nodeapp % npm i -g mysql
+ mysql@2.18.1
added 11 packages from 15 contributors in 0.589s
thodoristhomaidis@Theos-iMac nodeapp % npm start

> nodeapp@0.0.0 start /Volumes/HDD 500GB/* Documents/2 webdev/projects/nodejs/db connection demo/nodeapp
> node ./bin/www

Database is connected successfully !
GET /users/form 304 7.634 ms - -
GET /css/form-style.css 404 3.291 ms - 1532
events.js:291
      throw er; // Unhandled 'error' event
      ^

Error: Connection lost: The server closed the connection.
    at Protocol.end (/Volumes/HDD 500GB/* Documents/2 webdev/projects/nodejs/db connection demo/nodeapp/node_modules/mysql/lib/protocol/Protocol.js:112:13)
    at Socket.<anonymous> (/Volumes/HDD 500GB/* Documents/2 webdev/projects/nodejs/db connection demo/nodeapp/node_modules/mysql/lib/Connection.js:94:28)
    at Socket.<anonymous> (/Volumes/HDD 500GB/* Documents/2 webdev/projects/nodejs/db connection demo/nodeapp/node_modules/mysql/lib/Connection.js:526:10)
    at Socket.emit (events.js:326:22)
    at endReadableNT (_stream_readable.js:1226:12)
    at processTicksAndRejections (internal/process/task_queues.js:80:21)
Emitted 'error' event on Connection instance at:
    at Connection._handleProtocolError (/Volumes/HDD 500GB/* Documents/2 webdev/projects/nodejs/db connection demo/nodeapp/node_modules/mysql/lib/Connection.js:423:8)
    at Protocol.emit (events.js:314:20)
    at Protocol._delegateError (/Volumes/HDD 500GB/* Documents/2 webdev/projects/nodejs/db connection demo/nodeapp/node_modules/mysql/lib/protocol/Protocol.js:398:10)
    at Protocol.end (/Volumes/HDD 500GB/* Documents/2 webdev/projects/nodejs/db connection demo/nodeapp/node_modules/mysql/lib/protocol/Protocol.js:116:8)
    at Socket.<anonymous> (/Volumes/HDD 500GB/* Documents/2 webdev/projects/nodejs/db connection demo/nodeapp/node_modules/mysql/lib/Connection.js:94:28)
    [... lines matching original stack trace ...]
    at processTicksAndRejections (internal/process/task_queues.js:80:21) {
  fatal: true,
  code: 'PROTOCOL_CONNECTION_LOST'
}
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! nodeapp@0.0.0 start: `node ./bin/www`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the nodeapp@0.0.0 start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /Users/thodoristhomaidis/.npm/_logs/2021-01-22T03_11_13_709Z-debug.log

1 个答案:

答案 0 :(得分:0)

尝试使用 this code 来处理服务器断开连接:

var db_config = {
  host: 'localhost',
    user: 'root',
    password: '',
    database: 'example'
};

var connection;

function handleDisconnect() {
  connection = mysql.createConnection(db_config); // Recreate the connection, since
                                                  // the old one cannot be reused.

  connection.connect(function(err) {              // The server is either down
    if(err) {                                     // or restarting (takes a while sometimes).
      console.log('error when connecting to db:', err);
      setTimeout(handleDisconnect, 2000); // We introduce a delay before attempting to reconnect,
    }                                     // to avoid a hot loop, and to allow our node script to
  });                                     // process asynchronous requests in the meantime.
                                          // If you're also serving http, display a 503 error.
  connection.on('error', function(err) {
    console.log('db error', err);
    if(err.code === 'PROTOCOL_CONNECTION_LOST') { // Connection to the MySQL server is usually
      handleDisconnect();                         // lost due to either server restart, or a
    } else {                                      // connnection idle timeout (the wait_timeout
      throw err;                                  // server variable configures this)
    }
  });
}

handleDisconnect();

在您的代码中,我遗漏了 connection = mysql.createConnection(db_config);

之后的部分

你能检查一下this

相关问题