2013-05-07 212 views
0

以下是我的nodejs腳本。突然跑起來:sudo NODE_ENV=test node server.js給出:從cli運行nodejs給出錯誤ENOTFOUND

info - socket.io started 
{ [Error: getaddrinfo ENOTFOUND] 
    code: 'ENOTFOUND', 
    errno: 'ENOTFOUND', 
    syscall: 'getaddrinfo', 
    fatal: true 
} 

我該怎麼做調試?

var iniparser = require('iniparser'); 
var config = iniparser.parseSync('../Config/init/db.ini'); 
var env = process.env.NODE_ENV || 'dev'; //startup nodejs with e.g: NODE_ENV=test forever start server.js 

var app = require('http').createServer(handler), 
    io = require('socket.io').listen(app), 
    fs = require('fs'), 
    mysql = require('mysql'), 
    connectionsArray = [], 
    connection = mysql.createConnection({ 
     host: config[env]['host'], 
     user: config[env]['user'], 
     password: config[env]['pwd'].replace(/"/g, ''), 
     database: config[env]['dbname'] 
    }), 
    POLLING_INTERVAL = 1000, 
    pollingTimer; 

// If there is an error connecting to the database 
connection.connect(function (err) { 
// connected! (unless `err` is set) 
    console.log(err); 
}); 


// creating the server (localhost:8000) 
app.listen(1443); 

// on server started we can load our client.html page 
function handler(req, res) { 
    fs.readFile(__dirname + '/client.html', function (err, data) { 
     if (err) { 
      console.log(err); 
      res.writeHead(500); 
      return res.end('Error loading client.html'); 
     } 
     res.writeHead(200); 
     res.end(data); 
    }); 
} 

/* 
* 
* HERE IT IS THE COOL PART 
* This function loops on itself since there are sockets connected to the page 
* sending the result of the database query after a constant interval 
* 
*/ 
var pollingLoop = function() { 

// Doing the database query ap.line_state != 0 means busy 
    var query = connection.query('SELECT p.id, ap.* FROM active_player ap LEFT JOIN player p ON ap.dossier_id=p.dossier_id '), 

     players = []; // this array will contain the result of our db query 

// setting the query listeners 
    query 
     .on('error', function (err) { 
// Handle error, and 'end' event will be emitted after this as well 
      console.log(err); 
      console.log("ENDPOINT", this.request.httpRequest.endpoint); 
      updateSockets(err); 
     }) 
     .on('result', function (player) { 
// it fills our array looping on each user row inside the db 
      players.push(player); 
     }) 
     .on('end', function() { 
// loop on itself only if there are sockets still connected 

      if (connectionsArray.length) { 
       pollingTimer = setTimeout(pollingLoop, POLLING_INTERVAL); 
       updateSockets({players: players}); 
      } 

     }); 

}; 

function is(a, b) { 
    return a === b && (a !== 0 || 1/a === 1/b) // false for +0 vs -0 
     || a !== a && b !== b; // true for NaN vs NaN 
} 

// creating a new websocket to keep the content updated without any AJAX request 
io.sockets.on('connection', function (socket) { 

    console.log('Number of connections:' + connectionsArray.length); 
// starting the loop only if at least there is one user connected 
    if (!connectionsArray.length) { 
     pollingLoop(); 
    } 

    socket.on('disconnect', function() { 
     var socketIndex = connectionsArray.indexOf(socket); 
     console.log('socket = ' + socketIndex + ' disconnected'); 
     if (socketIndex >= 0) { 
      connectionsArray.splice(socketIndex, 1); 
     } 
    }); 

    console.log('A new socket is connected!'); 
    connectionsArray.push(socket); 

}); 

var updateSockets = function (data) { 
// adding the time of the last update 
    data.time = new Date(); 
// sending new data to all the sockets connected 
    connectionsArray.forEach(function (tmpSocket) { 
     tmpSocket.volatile.emit('notification', data); 
    }); 
}; 
+0

你有沒有在您的配置測試環境? – 2013-05-07 16:13:00

+0

看起來像你的數據庫配置是錯誤的。我使用正確的數據庫證書運行此代碼 - 正確工作 – Eugene 2013-05-07 16:16:46

回答

0

爲了獲得充分的信息有關的異常取代

console.log(err); 

要:

console.err(err.stack) 

以及在connecttion.connect回調函數ERR變量添加如果。

你也可以添加該代碼,safy捕獲所有異常:

process.on('uncaughtException', function globalErrorCatch(error, p){ 
    console.error(error); 
    console.error(error.stack); 
}); 
+0

我添加了建議的調試代碼。現在我得到: '在Handshake.Sequence.end(/www/test-front/webroot/node_modules/mysql/lib/protocol/sequences/Sequence.js:66:24) at Protocol.handleNetworkError(/ www/test -front/webroot/node_modules/mysql/lib/protocol/Protocol.js:230:14) at Connection._handleNetworkError(/www/test-front/webroot/node_modules/mysql/lib/Connection.js:145:18) at Socket.EventEmitter.emit(events.js:117:20) at net.js:807:16' 它工作在localy,db證書沒問題。也許nodejs或其他包損壞? – Bart 2013-05-07 16:42:56

+0

嘗試移動io = require('socket.io')。listen(app)app.listen()之後。我在我的應用程序 – Eugene 2013-05-07 16:53:31

+0

看來,WebSocket的只是不寫使用這個命令: '調試 - 清除客戶端f0PHApuqQsxAcNonF1UV心跳超時 調試 - 客戶端設置心跳間隔f0PHApuqQsxAcNonF1UV 調試 - WebSocket的寫作 - 爲客戶f0PHApuqQsxAcNonF1UV 調試發射心跳2 :: 調試 - 爲客戶端設置心跳超時f0PHApuqQsxAcNonF1UV 調試 - 獲取心跳包 ' – Bart 2013-05-08 07:48:45

相關問題