Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I structure my express app where I only need to open a mongodb connection once?
    primarykey
    data
    text
    <p><strong>Note: Please read the edited portion of this post before answering, it might save you time and answers one of my questions.</strong></p> <p>The problem I'm having is pretty simple but I'm pretty new to this overall and I'm having issues figuring out how to implement a mongodb database connection properly in a node/express app.</p> <p>I'm using express 3.x and am basing my app layout around this project supplied by the author of express: <a href="https://github.com/visionmedia/express/tree/master/examples/blog">https://github.com/visionmedia/express/tree/master/examples/blog</a></p> <p>I have no interest in making a blog however the way the app is structured appears to be quite nice. The routes are separated and everything is organized nicely.</p> <p>My problem is I might have 5-6 different route js files and each route js file might have anywhere between 1 and 15 routes; of those routes 1 or 15 might want to access the db.</p> <p>So my problem is it seems like a really terrible idea to do a db.open(...) every single time I want to query the db. I should mention at this point I'm using the native mongo-db driver (npm install mongodb).</p> <p>I would also need to include a file like this: <a href="http://pastebin.com/VzFsPyax">http://pastebin.com/VzFsPyax</a></p> <p>...in all of those route files and all of my model files. Then I'm also dealing with dozens upon dozens of open connections.</p> <p>Is there a way I can structure my app in such a way where I only make 1 connection and it stays open for the duration of the session (having a new one made every request would be bad too)?</p> <p>If so, how can I do this? If you know the answer please post a code sample using tj's blog app (the one linked earlier in this post) structure as a base guide. Basically have a way where the routes and models can use the db freely while being in separate files than the db open code.</p> <p>Thanks.</p> <p><strong>EDIT</strong></p> <p>I made some progress on solving one of my issues. If you look at tj's blog example he initializes his routes in the app.js like so:</p> <pre><code>require('./routes/site')(app); require('./routes/post')(app); </code></pre> <p>And in the routes js file it starts like this:</p> <pre><code>module.exports = function(app){ </code></pre> <p>I stumbled on a project earlier today where I saw someone pass 2 variables in the modules.exports call -> function(app, db). Then figured wow could it be that easy, do I need to just adjust my routes to be (app, db) too? Yeah, it seems so.</p> <p>So now part 1 of the problem is solved. I don't have to require a mongo.js file with the connection boilerplate in every route file. At the same time it's flexible enough where I can decide to pick and choose which route files pass a db reference. This is standard and has no downside right?</p> <p>Part 2 of the problem (the important one unfortunately) still exists though.</p> <p>How can I bypass having to do a db.open(...) around every query I make and ideally only make a connection once per session?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload