-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathwebapp.js
64 lines (55 loc) · 1.74 KB
/
webapp.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
var express = require('express');
var bodyParser = require('body-parser');
var MongoClient = require('mongodb').MongoClient;
var ObjectId = require('mongodb').ObjectID;
var app = express();
var db;
app.use(express.static('static'));
/* Get a list of filtered records */
app.get('/api/bugs', function(req, res) {
console.log("Query string", req.query);
var filter = {};
if (req.query.priority)
filter.priority = req.query.priority;
if (req.query.status)
filter.status = req.query.status;
db.collection("bugs").find(filter).toArray(function(err, docs) {
res.json(docs);
});
});
app.use(bodyParser.json());
/* Insert a record */
app.post('/api/bugs/', function(req, res) {
console.log("Req body:", req.body);
var newBug = req.body;
db.collection("bugs").insertOne(newBug, function(err, result) {
var newId = result.insertedId;
db.collection("bugs").find({_id: newId}).next(function(err, doc) {
res.json(doc);
});
});
});
/* Get a single record */
app.get('/api/bugs/:id', function(req, res) {
db.collection("bugs").findOne({_id: ObjectId(req.params.id)}, function(err, bug) {
res.json(bug);
});
});
/* Modify one record, given its ID */
app.put('/api/bugs/:id', function(req, res) {
var bug = req.body;
console.log("Modifying bug:", req.params.id, bug);
var oid = ObjectId(req.params.id);
db.collection("bugs").updateOne({_id: oid}, bug, function(err, result) {
db.collection("bugs").find({_id: oid}).next(function(err, doc) {
res.send(doc);
});
});
});
MongoClient.connect('mongodb://localhost/bugsdb', function(err, dbConnection) {
db = dbConnection;
var server = app.listen(3000, function() {
var port = server.address().port;
console.log("Started server at port", port);
});
});