2015-04-16 3 views
0

Я строю свой первый сервер node.js для выполнения запроса mysql и возвращения результатов из базы данных. Все работает, но теперь я не могу найти правильный способ передать значение из URL-адреса (раздела запроса) функции, выполняющей запрос (функция PollingLoop). Нет проблем с поиском url и получить параметр в функции обработчика, но переместить его на pollingLoop. Я пробовал почти все, что мне известно о javascript (недостаточно я вижу). Это мой код, который не запускается из-за ошибки ссылки в pollingLoop для hwkey, которая не определена.node js server: проблема передачи переменной функции

var app = require('http').createServer(handler), 
    io = require('socket.io').listen(app), 
    url = require('url'), 
    fs = require('fs'), 
    mysql = require('mysql'), 
    connectionsArray = [], 
    connection = mysql.createConnection({ 
    host: 'localhost', 
    user: 'root', 
    password: 'flipper', 
    database: 'oclock', 
    port: 3306 
    }), 
    POLLING_INTERVAL = 3000, 
    pollingTimer; 

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

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


function handler(req, res) { 

    console.log("INCOMING REQUEST: "+req.method+" "+req.url); 
    req.parsed_url = url.parse(req.url, true); 
    var getp = req.parsed_url.query; 
    var hwkey = getp.hk; 
    console.log(hwkey); 

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


function pollingLoop(){ 
    // Doing the database query 
    var query = connection.query('SELECT max(id), testo, created_by FROM flashmsgs WHERE hwk="'+hwkey+'"'), 
    //var query = connection.query('SELECT max(id), testo, created_by FROM flashmsgs'), 
    flashmsgs = []; // 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); 
     updateSockets(err); 
    }) 
    .on('result', function(flashmsg) { 
     // it fills our array looping on each user row inside the db 
     flashmsgs.push(flashmsg); 
    }) 
    .on('end', function() { 
     // loop on itself only if there are sockets still connected 
     if (connectionsArray.length) { 

     pollingTimer = setTimeout(pollingLoop, POLLING_INTERVAL); 

     updateSockets({ 
      flashmsgs: flashmsgs 
     }); 
     } else { 

     console.log('The server timer was stopped because there are no more socket connections on the app') 

     } 
    }); 
}; 


// 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('socketID = %s got disconnected', socketIndex); 
    if (~socketIndex) { 
     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(); 
    console.log('Pushing new data to the clients connected (connections amount = %s) - %s', connectionsArray.length , data.time); 
    // sending new data to all the sockets connected 
    connectionsArray.forEach(function(tmpSocket) { 
    tmpSocket.volatile.emit('notification', data); 
    }); 
}; 

console.log('Please use your browser to navigate to http://localhost:8000'); 
+0

просто принести на hwkey переменную из из обработчика 'вар hwkey; обработчик функции (req, res) {hwkey = ...} pollingLoop() {console.log (hwkey);} ' –

+0

Спасибо. Оно работает. Я тоже пробовал, но определял var hwkey = ''; и не получил результата. Thnak вас так много. Поверните комментарий в ответ, чтобы подтвердить его! –

ответ

1

просто принести на hwkey переменную из обработчика

var hwkey; 
function handler(req, res) { hwkey = ... } 
pollingLoop(){console.log(hwkey);} 
Смежные вопросы