I am playing with nodejs and sqlite3 The server will be used for making rest calls using express.
Being kind of new to node, i have two questions on how to manage the db connection.
1 ) Do i open/connect to the
var db = new sqlite3.Database(file);
before the listener and then close the connection on exit of the server or in each route i open the connection and close when the rest call is complete.
2 ) how do i pass the connection to a separate route file? I tried based on some examples, but get the error message "Sever initialization failed Cannot call method 'indexOf' of undefined" (see code below the exception)
Trace
at Object.<anonymous> (c:\Work2\server.js:154:13) at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Module.runMain [as _onTimeout] (module.js:497:10)
at Timer.listOnTimeout [as ontimeout] (timers.js:112:15)
these are
//server.js
var db = new sqlite3.Database(file);
var apiRoutes = require('./routes/apiRoutes')(db);
//apiRoutes.js
module.exports = (function (db) {
router.get('/userlist', function (req, res, next) {
res.json('{stuff...}');
});
return router;
})();
Thanks for any help
Aucun commentaire:
Enregistrer un commentaire