I'm using gridfs-stream with mongoose. The docs say to connect using:
var conn = mongoose.createConnection();
conn.once("open", function () {
var gfs = Grid(conn.db, mongoose.mongo);
});
I assume that it's preferred to reuse one Grid (gfs
above) throughout the app. (True?) Accordingly, I have a module gridfs.js
containing
module.exports = Grid(mongoose.connection.db, mongoose.mongo);
which, relying on node's module caching, will reuse the Grid. However, my main file (app.js) is structured like this:
app.use("/path1", require("controllers/path1.js"));
// ...
mongoose.connect(...);
mongoose.connection.once("open", function () {
app.listen(port);
});
Issue is: the controller required on the first line of the snippet has a require("gridfs.js")
line, and that comes before mongoose connects.
Questions:
Is it proper to reuse a Grid throughout an app, or should I create one in each route handler? (Note that it would have to be in the route handler callback itself, not at the top of the module, for the above reason.)
If reuse is proper, what's the right way to fix the above problem? Load all my routes/controllers in the
mongoose.connection.once("open")
callback?
Aucun commentaire:
Enregistrer un commentaire