避免 Node.js 应用程序因 node-mysql 错误而崩溃
Avoid Node.js application from crashing with node-mysql error
在我创建的场景中,我关闭了数据库并尝试执行如下查询:
router.get('/manga/updatescores', function(req, res) {
pool.getConnection(function(err, connection) {
connection.query('select * from foobar', function(err, rows) {
// ...
}
});
// ...
}
并得到如下日志:
C:\Users\Void\Documents\node-training\test-mysql\controller\routes.js:201
connection.query('select * from foobar', function(err, rows) {
^
TypeError: Cannot read property 'query' of undefined
at C:\Users\Void\Documents\node-training\test-mysql\controller\routes.js:201:14
at Handshake.onConnect (C:\Users\Void\Documents\node-training\test-mysql\node_modules\mysql\lib\Pool.js:54:9)
at Handshake.Sequence.end (C:\Users\Void\Documents\node-training\test-mysql\node_modules\mysql\lib\protocol\sequences\Sequence.js:96:24)
at Protocol.handleNetworkError (C:\Users\Void\Documents\node-training\test-mysql\node_modules\mysql\lib\protocol\Protocol.js:364:14)
at PoolConnection.Connection._handleNetworkError (C:\Users\Void\Documents\node-training\test-mysql\node_modules\mysql\lib\Connection.js:421:18)
at emitOne (events.js:77:13)
at Socket.emit (events.js:169:7)
at emitErrorNT (net.js:1253:8)
at nextTickCallbackWith2Args (node.js:442:9)
at process._tickCallback (node.js:356:17)
npm ERR! Windows_NT 6.3.9600
npm ERR! argv "C:\Program Files\nodejs\node.exe" "C:\Program Files\nodejs\node_modules\npm\bin\npm-cli.js" "start"
npm ERR! node v4.4.3
npm ERR! npm v2.15.1
npm ERR! code ELIFECYCLE
npm ERR! test-mysql@1.0.0 start: `node server.js`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the test-mysql@1.0.0 start script 'node server.js'.
npm ERR! This is most likely a problem with the test-mysql package,
npm ERR! not with npm itself.
npm ERR! Tell the author that this fails on your system:
npm ERR! node server.js
npm ERR! You can get information on how to open an issue for this project with:
npm ERR! npm bugs test-mysql
npm ERR! Or if that isn't available, you can get their info via:
npm ERR!
npm ERR! npm owner ls test-mysql
npm ERR! There is likely additional logging output above.
npm ERR! Please include the following file with any support request:
npm ERR! C:\Users\Void\Documents\node-training\test-mysql\npm-debug.log
出于某种原因,此错误只会停止服务器。当然,我有几种方法来处理这个错误。但我想知道的是,是否有某种用于 Express 的中间件可以避免有人没有预见到停止服务器的未捕获错误?
谢谢!
您可以使用 'promise-mysql' 并捕获所有异常。
参考:https://github.com/lukeb-uk/node-promise-mysql
var mysql = require('promise-mysql');
var connection;
mysql.createConnection({
host: 'localhost',
user: 'sauron',
password: 'theonetruering',
database: 'mordor'
}).then(function(conn){
connection = conn;
}).catch(function(error){
//logs out the error
console.log(error);
});
在我创建的场景中,我关闭了数据库并尝试执行如下查询:
router.get('/manga/updatescores', function(req, res) {
pool.getConnection(function(err, connection) {
connection.query('select * from foobar', function(err, rows) {
// ...
}
});
// ...
}
并得到如下日志:
C:\Users\Void\Documents\node-training\test-mysql\controller\routes.js:201
connection.query('select * from foobar', function(err, rows) {
^
TypeError: Cannot read property 'query' of undefined
at C:\Users\Void\Documents\node-training\test-mysql\controller\routes.js:201:14
at Handshake.onConnect (C:\Users\Void\Documents\node-training\test-mysql\node_modules\mysql\lib\Pool.js:54:9)
at Handshake.Sequence.end (C:\Users\Void\Documents\node-training\test-mysql\node_modules\mysql\lib\protocol\sequences\Sequence.js:96:24)
at Protocol.handleNetworkError (C:\Users\Void\Documents\node-training\test-mysql\node_modules\mysql\lib\protocol\Protocol.js:364:14)
at PoolConnection.Connection._handleNetworkError (C:\Users\Void\Documents\node-training\test-mysql\node_modules\mysql\lib\Connection.js:421:18)
at emitOne (events.js:77:13)
at Socket.emit (events.js:169:7)
at emitErrorNT (net.js:1253:8)
at nextTickCallbackWith2Args (node.js:442:9)
at process._tickCallback (node.js:356:17)
npm ERR! Windows_NT 6.3.9600
npm ERR! argv "C:\Program Files\nodejs\node.exe" "C:\Program Files\nodejs\node_modules\npm\bin\npm-cli.js" "start"
npm ERR! node v4.4.3
npm ERR! npm v2.15.1
npm ERR! code ELIFECYCLE
npm ERR! test-mysql@1.0.0 start: `node server.js`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the test-mysql@1.0.0 start script 'node server.js'.
npm ERR! This is most likely a problem with the test-mysql package,
npm ERR! not with npm itself.
npm ERR! Tell the author that this fails on your system:
npm ERR! node server.js
npm ERR! You can get information on how to open an issue for this project with:
npm ERR! npm bugs test-mysql
npm ERR! Or if that isn't available, you can get their info via:
npm ERR!
npm ERR! npm owner ls test-mysql
npm ERR! There is likely additional logging output above.
npm ERR! Please include the following file with any support request:
npm ERR! C:\Users\Void\Documents\node-training\test-mysql\npm-debug.log
出于某种原因,此错误只会停止服务器。当然,我有几种方法来处理这个错误。但我想知道的是,是否有某种用于 Express 的中间件可以避免有人没有预见到停止服务器的未捕获错误?
谢谢!
您可以使用 'promise-mysql' 并捕获所有异常。 参考:https://github.com/lukeb-uk/node-promise-mysql
var mysql = require('promise-mysql');
var connection;
mysql.createConnection({
host: 'localhost',
user: 'sauron',
password: 'theonetruering',
database: 'mordor'
}).then(function(conn){
connection = conn;
}).catch(function(error){
//logs out the error
console.log(error);
});