Created
June 26, 2015 12:37
-
-
Save connors511/3ca61908b29f54a2aef2 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
// 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