Reputation: 3221
I'm an Express rookie, and don't understand: Why does the following code never return? It just seems to hang.
router.post('/post',function(req,res){
var file = "test.db"
var exists = fs.existsSync(file);
if(!exists) {
console.log("Creating DB file.");
fs.openSync(file, "w");
}
var sqlite3 = require("sqlite3").verbose();
var db = new sqlite3.Database(file);
db.serialize(function() {
if(!exists) {
db.run("CREATE TABLE Stuff (thing TEXT)");
}
var stmt = db.prepare("INSERT INTO Stuff(thing) VALUES (99999)");
stmt.finalize();
});
db.close();
}); //seems to hang at this line.
Upvotes: 1
Views: 242
Reputation: 3586
You need to invoke res.send or res.end to return something from request and prevent it from hanging.
router.post('/post',function(req,res){
var file = "test.db"
var exists = fs.existsSync(file);
if(!exists) {
console.log("Creating DB file.");
fs.openSync(file, "w");
}
var sqlite3 = require("sqlite3").verbose();
var db = new sqlite3.Database(file);
db.serialize(function() {
if(!exists) {
db.run("CREATE TABLE Stuff (thing TEXT)");
}
var stmt = db.prepare("INSERT INTO Stuff(thing) VALUES (99999)");
stmt.finalize();
});
db.close();
res.send("<h1>Done</h1>");
}); //seems to hang at this line.
Upvotes: 1