Best Practice to Open/Close MongoDB Connection in Node.js Web App?

8,266 views
Skip to first unread message

David Graf

unread,
Jul 8, 2011, 2:05:02 AM7/8/11
to node-mong...@googlegroups.com
Hello

What is the best practice to open and close a MongoDB connection in a node.js web app? Currently, I am opening the connection and I am starting the webserver in the corresponding callback function:

  db.open(function(err, db) {
    http.createServer(onRequest).listen(8888);
    console.log("Server has started.");
  }); 

Is this best practice? And where should I close the connection? Is closing the connection not necessary?

Best
David

Callum Macrae

unread,
Jul 8, 2011, 2:43:38 AM7/8/11
to node-mong...@googlegroups.com
That is absolutely fine. You don't need to close any connections anywhere.

Callum Macrae

Gui HGO

unread,
Dec 1, 2016, 9:23:03 PM12/1/16
to node-mongodb-native, callum...@gmail.com
thanks.
Reply all
Reply to author
Forward
0 new messages