从cli运行nodejs会产生错误ENOTFOUND

时间:2013-05-07 16:01:52

标签: node.js

下面是我的nodejs脚本。突然用sudo NODE_ENV=test node server.js运行它:

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);
    });
};

1 个答案:

答案 0 :(得分:0)

获取有关例外替换的完整信息

console.log(err);

要:

console.err(err.stack)

并在connecttion.connect回调函数中为if变量添加if。

此外,您可以添加此代码,以便安全捕获所有异常:

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