I have the following 2 problems when querying sqlite3 database with node.js via socket connection:
with the node.js http client. I am requesting a node.js HTTP server, the sequence of results is mixed
from time to time I get socket forks, it doesn't play
Here is the server code:
var http = require('http'); var url = require('url'); var sqlite3 = require('sqlite3').verbose(); var counter = 0; //create sqlite database for testing var db = new sqlite3.Database("db.sqlite3"); db.serialize(function() { db.run("DROP TABLE IF EXISTS test"); db.run("CREATE TABLE IF NOT EXISTS test (ID INTEGER)"); var stmt = db.prepare("INSERT INTO test VALUES (?)"); for (var i = 1; i <= 10; i++) { stmt.run(i); } stmt.finalize(); }); db.close(); //run server http.createServer(function (req, res) { var queryData = url.parse(req.url, true).query; res.writeHead(200, {"Content-Type": "text/plain; charset=utf-8"});//, "connection": "keep-alive" var db = new sqlite3.Database(new Buffer(queryData.SQLServerAddress, "base64").toString('utf8')); db.serialize(function() { db.each(new Buffer(queryData.SQLStatement, "base64").toString('utf8'), function(err, row) { counter++; console.log(counter + " / " + JSON.stringify(row)); res.end(JSON.stringify(row).toString()); }); }); db.close(); counter = 0; }).listen(1337, '127.0.0.1'); console.log('Server running...');
And here is the client code:
var http = require('http'); for(i=1;i<=10;i++){ var SQLServerAddress = new Buffer("db.sqlite3").toString('base64'); var SQLStatement = new Buffer("SELECT * FROM test WHERE ID=" + i).toString('base64'); var options = { host: "127.0.0.1", port: 1337, path: "/?SQLServerAddress=" + SQLServerAddress + "&SQLStatement=" + SQLStatement }; callback = function(response){ var str = ""; response.on("data", function (chunk){ str += chunk; }); response.on("end", function (){ console.log(str); }); } http.request(options, callback).end(); }
I repeat the request 10 times as a "stress test", sort of ...
So what I get in the console, for example,
Server Console:
1 / {"ID":1} 2 / {"ID":2} 3 / {"ID":5} 4 / {"ID":4} 1 / {"ID":3} 2 / {"ID":6} 1 / {"ID":7} 2 / {"ID":9} 3 / {"ID":10} 4 / undefined
And the client console:
{"ID":1} {"ID":2} {"ID":5} {"ID":4} {"ID":3} {"ID":6} {"ID":7} {"ID":9} {"ID":10} events.js:68 throw arguments[1];
My questions:
- How to save the results in the correct order?
- What can I do to prevent the socket from hanging?
Thanks in advance!
source share