-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
72 lines (62 loc) · 1.94 KB
/
app.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
var express = require('express');
var bodyParser = require('body-parser');
// The express app
var app = express();
// Create a mongodb connection
// and only start express listening once the connection is okay
var mongodb = require('mongodb');
var db, itemsCollection;
mongodb.MongoClient.connect('mongodb://127.0.0.1:27017/demo', function (err, database) {
if (err) throw err;
// Connected!
db = database;
itemsCollection = db.collection('items');
app.listen(8080);
console.log('Listening on port 8080');
});
// Create a router that can accept JSON
var router = express.Router();
router.use(bodyParser.json());
// Setup the collection routes
router.route('/')
.get(function (req, res, next) {
itemsCollection.find().toArray(function (err, docs) {
res.send({
status: 'Items found',
items: docs
});
});
})
.post(function (req, res, next) {
var item = req.body;
itemsCollection.insert(item, function (err, docs) {
res.send({
status: 'Item added',
itemId: item._id
});
});
})
// Setup the item routes
router.route('/:id')
.delete(function (req, res, next) {
var id = req.params['id'];
var lookup = { _id: new mongodb.ObjectID(id) };
itemsCollection.remove(lookup, function (err, results) {
res.send({ status: 'Item cleared' });
});
});
router.route('/:id/:barcode')
.get(function (req, res, next) {
var id = req.params['id'];
var newBarcode = req.params['barcode'];
var lookup = {
_id: new mongodb.ObjectID(id),
barcode: newBarcode
};
itemsCollection.save(lookup, function (err, docs) {
res.send({ status: 'Item update' });
});
});
//app.use(express.static(__dirname + '/public'))
app.use(express.static(__dirname))
.use('/tea', router);