-
Notifications
You must be signed in to change notification settings - Fork 3
/
books.js
41 lines (35 loc) · 795 Bytes
/
books.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
var app = require('express')();
var mongo = require('mongodb');
var db;
app.get('/', function (req, res) {
var collection = db.collection('books');
// Find some documents
collection.find({}).toArray(function(err, docs) {
if (err) {
console.error(err);
return res.send({success: false });
}
res.send({
success: true,
books: docs
});
});
});
app.get('/:id', function (req, res) {
var collection = db.collection('books');
var objId = new mongo.ObjectID(req.params.id);
collection.findOne({ _id: objId }, function(err, doc) {
if (err) {
console.error(err);
return res.send({success: false });
}
res.send({
success: true,
book: doc
});
});
});
module.exports = function (mongo) {
db = mongo;
return app;
}