Skip to content

Instantly share code, notes, and snippets.

@connors511
Created June 26, 2015 12:37
Show Gist options
  • Save connors511/3ca61908b29f54a2aef2 to your computer and use it in GitHub Desktop.
Save connors511/3ca61908b29f54a2aef2 to your computer and use it in GitHub Desktop.
// Global scope, for static list after first boot
books = [];
var findBooks = function(db, callback) {
var cursor = db.collection('books').find();
cursor.each(function(err, doc) {
assert.equal(err, null);
if (doc !== null) {
books.push(doc);
} else {
callback();
}
});
};
// Open connection
MongoClient.connect(url, function(err, db) {
assert.equal(null, err);
// Fetch all books
findBooks(db, function() {
db.close();
// httpServer.listen or similar
// First start listening when you've loaded the data
app.listen(80);
});
});
// Du kan godt definere alle routes her
app.get('/list', function(req, res) {
res.render('list', { books: books });
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment