error handling - Why Node.js stop working? -
i installed node.js on server , it's working. stops after while error:
events.js:77 throw er; // unhandled 'error' event ^ error: connection lost: server closed connection. @ protocol.end (/var/www/node/node_modules/mysql/lib/protocol/protocol.js:73:13) @ socket.onend (stream.js:79:10) @ socket.eventemitter.emit (events.js:122:20) @ _stream_readable.js:910:16 @ process._tickcallback (node.js:373:11) error: forever detected script exited code: 8 error: forever restarting script 14 time
the run node.js on port 8000
socket.io
, node-mysql
, mc
.
the file path events.js
/node/lib/events.js
.
if use forever
can run continuously error still comes up. it's restart script. not best solution (better nothing, maybe worst solution).
i'm gonna give try uncaughtexception
still not best solution. code:
process.on('uncaughtexception', function (err) { console.log('caught exception: ' + err); });
please me out if can. thanks.
you need handle error event on mysql connection. if event emitter emits "error" event , not handled, exception thrown. not sure doing in code, see below how should handling this:
var mysql = require('mysql'); var connection = mysql.createconnection({ host : 'localhost', user : 'me', password : 'secret', }); connection.on('error', function (err) { // handle error here. }); connection.connect(); connection.query('select 1 + 1 solution', function(err, rows, fields) { if (err) throw err; console.log('the solution is: ', rows[0].solution); }); connection.end();
here example of handling disconnects https://github.com/felixge/node-mysql/blob/master/readme.md#server-disconnects:
function handledisconnect() { connection = mysql.createconnection(db_config); // recreate connection, since // old 1 cannot reused. connection.connect(function(err) { // server either down if(err) { // or restarting (takes while sometimes). console.log('error when connecting db:', err); settimeout(handledisconnect, 2000); // introduce delay before attempting reconnect, } // avoid hot loop, , allow our node script }); // process asynchronous requests in meantime. // if you're serving http, display 503 error. connection.on('error', function(err) { console.log('db error', err); if(err.code === 'protocol_connection_lost') { // connection mysql server handledisconnect(); // lost due either server restart, or } else { // connnection idle timeout (the wait_timeout throw err; // server variable configures this) }); } handledisconnect();
Comments
Post a Comment