Following the example of my comment, changing it so that the application handles errors rather than starting the server.
var express = require('express');
var mongodb = require('mongodb');
var app = express();
var MongoClient = require('mongodb').MongoClient;
var db;
MongoClient.connect("mongodb://localhost:27017/integration_test", function(err, database) {
if(err) return console.error(err);
db = database;
});
app.get("/", function(req, res, next) {
db.collection("replicaset_mongo_client_collection").find({}, function(err, docs) {
if(err) return next(err);
docs.each(function(err, doc) {
if(doc) {
console.log(doc);
}
else {
res.end();
}
});
});
});
app.use(function(err, req, res){
});
app.listen(3000);
console.log("Listening on port 3000");
source
share