I'm using node-mysql. Below codes taken from http://blog.josedacruz.com/2013/07/19/learning-node-js-using-mysql/
var http = require('http');
var mysql = require('mysql');
var connection = mysql.createConnection({
host : 'localhost',
port : 3306,
database: 'aaa',
user : 'root',
password : 'z'
});
http.createServer(function(req, res){
res.writeHeader(200);
res.write('Connect to mySql\n');
// Connect to mySql (if there is an erro, report it and terminate de request)
connection.connect(function(err){
if(err != null) {
res.end('Error connecting to mysql:' + err+'\n');
}
});
connection.query("SELECT value FROM variable WHERE name = 'site_name'", function(err, rows){
if(err != null) {
res.end("Query error:" + err);
} else {
// Shows the result on console window
console.log(rows[0]);
res.end("Success!");
}
// Close connection
connection.end();
});
}).listen(80);
First thing first, I feel strange the connection.end not same level with connection.connect
I've tried change the connection to be same level and also put on different place, but still no luck
Below is the error in terminal when HTTP server stops after first run (note: MyTube is the result of query)
{ value: 'MyTube' }
Error: Cannot enqueue Quit after invoking quit.
at Protocol._validateEnqueue (/home/mydir/node/node_modules/mysql/lib/protocol/Protocol.js:131:16)
at Protocol._enqueue (/home/mydir/node/node_modules/mysql/lib/protocol/Protocol.js:98:13)
at Protocol.quit (/home/mydir/node/node_modules/mysql/lib/protocol/Protocol.js:61:36)
at Connection.end (/home/mydir/node/node_modules/mysql/lib/Connection.js:155:18)
at Query._callback (/home/mydir/node/app.js:31:20)
at Query.Sequence.end (/home/mydir/node/node_modules/mysql/lib/protocol/sequences/Sequence.js:75:24)
at Protocol._validateEnqueue (/home/mydir/node/node_modules/mysql/lib/protocol/Protocol.js:153:6)
at Protocol._enqueue (/home/mydir/node/node_modules/mysql/lib/protocol/Protocol.js:98:13)
at Connection.query (/home/mydir/node/node_modules/mysql/lib/Connection.js:140:25)
at Server.<anonymous> (/home/mydir/node/app.js:22:16)
--------------------
at Quit.Sequence (/home/mydir/node/node_modules/mysql/lib/protocol/sequences/Sequence.js:15:20)
at new Quit (/home/mydir/node/node_modules/mysql/lib/protocol/sequences/Quit.js:8:12)
at Protocol.quit (/home/mydir/node/node_modules/mysql/lib/protocol/Protocol.js:61:45)
at Connection.end (/home/mydir/node/node_modules/mysql/lib/Connection.js:155:18)
at Query._callback (/home/mydir/node/app.js:31:20)
at Query.Sequence.end (/home/mydir/node/node_modules/mysql/lib/protocol/sequences/Sequence.js:75:24)
at Protocol._validateEnqueue (/home/mydir/node/node_modules/mysql/lib/protocol/Protocol.js:153:6)
at Protocol._enqueue (/home/mydir/node/node_modules/mysql/lib/protocol/Protocol.js:98:13)
at Connection.query (/home/mydir/node/node_modules/mysql/lib/Connection.js:140:25)
at Server.<anonymous> (/home/mydir/node/app.js:22:16)