Skip to content

Instantly share code, notes, and snippets.

@chrisckchang
Last active December 24, 2015 06:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chrisckchang/6757203 to your computer and use it in GitHub Desktop.
Save chrisckchang/6757203 to your computer and use it in GitHub Desktop.
example express app without connection pooling (calls mongoclient in each function)
var express = require('express');
var mongodb = require('mongodb');
var app = express();
var MONGODB_URI = 'mongo-uri';
app.get('/', function(req, res) {
// BAD! Creates a new connection pool for every request
mongodb.MongoClient.connect(MONGODB_URI, function(err, db) {
if(err) throw err;
var coll = db.collection('test');
coll.find({}, function(err, docs) {
docs.each(function(err, doc) {
if(doc) {
res.write(JSON.stringify(doc) + "\n");
}
else {
res.end();
}
});
});
});
});
// App may initialize before DB connection is ready
app.listen(3000);
console.log('Listening on port 3000');
@surfjedi
Copy link

I'm just trying to get this working with a Heroku node.js app. But I'm not familiar with ./logger ? can you send an example without?

@chrisckchang
Copy link
Author

Hi @surfjedi,

you can just remove line 3 and { server: { logger: logger(MONGODB_URI) } } from the connect() and it will work properly! Please note that this example is what you DON'T want to do. You can find my example of the proper way to connection pool here: https://gist.github.com/chrischang12/6757209

Feel free to email me anytime if you have questions

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment