2013-05-07 5 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

당신이 당신의 구성에서 테스트 환경을 가지고 있습니까 :

은 또한 당신은 모든 예외 safy 잡기 위해,이 코드를 추가 할 수 있습니까? –

+0

DB 구성이 잘못되었습니다. 올바른 DB 자격 증명을 사용하여이 코드를 실행합니다. – Eugene

답변

0

예외에

console.log(err); 

교체에 대한 완전한 정보를 얻을 경우 :

console.err(err.stack) 

그리고 connecttion.connect 콜백 함수에서 ERR 변수 경우 추가합니다.

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

제안 된 디버그 코드를 추가했습니다. 이제 얻을 수 있습니다 :'at 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' 로컬에서 작동합니다. db 자격 증명은 정상입니다. 어쩌면 노드 또는 다른 패키지가 손상되었을 수 있습니까? – Bart

+0

move io = require ('socket.io'). app.listen() 다음에 listen (app). 내 애플 리케이션 그것은, 웹 소켓은 단지 기록되지 않은 것 같다 – Eugene

+0

을이 순서를 사용 '디버그 - 웹 소켓 쓰기 - 클라이언트 f0PHApuqQsxAcNonF1UV 디버그를위한 발광 하트 비트 - 클라이언트에 대한 설정 하트 비트 간격 디버그를 f0PHApuqQsxAcNonF1UV - 클라이언트 f0PHApuqQsxAcNonF1UV에 대한 하트 비트 제한 시간이 디버그 클리어 2 : 디버그 - 클라이언트에 대한 설정 하트 비트 제한 시간은 디버그를 f0PHApuqQsxAcNonF1UV - 하트 비트 패킷을 가진 모든 반응에 대한 ' – Bart

관련 문제