A simple question, I use nodejs for HTTP restful API service. Now I want query some mysql data before http server start, the code snippet:
var price = -1;
var userId = 'some user provided value';
var sql = 'SELECT max(price) FROM users WHERE id = ' + connection.escape(userId);
connection.query(sql, function(err, results) {
price = results.price;
});
server = http.createServer(function (request, response) {
handler(request); //response depend on price
}).listen(8012);
So how to combine mysql query and http server code?
You just add the server inside of the return callback for the connection query, like so: