Last active
December 24, 2015 06:29
-
-
Save chrisckchang/6757203 to your computer and use it in GitHub Desktop.
example express app without connection pooling (calls mongoclient in each function)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); |
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
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?