-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
81 lines (71 loc) · 2.01 KB
/
server.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
72
73
74
75
76
77
78
79
80
81
'use strict'
let express = require('express');
let bodyParser = require('body-parser');
let MongoClient = require('mongodb').MongoClient;
let ObjectId = require('mongodb').ObjectID;
let app = express();
let 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);
let 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);
let newBug = req.body;
db.collection("bugs").insertOne(newBug, function(err, result) {
if (err) console.log(err);
let newId = result.insertedId;
db.collection("bugs").find({_id: newId}).next(function(err, doc) {
if (err) console.log(err);
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) {
let bug = req.body;
// ensure we don't have the _id itself as a field, it's disallowed to modfiy the
// _id.
delete (bug._id);
console.log("Modifying bug:", req.params.id, bug);
let oid = ObjectId(req.params.id);
db.collection("bugs").updateOne({_id: oid}, bug, function(err, result) {
if (err) console.log(err);
db.collection("bugs").find({_id: oid}).next(function(err, doc) {
if (err) console.log(err);
res.send(doc);
});
});
});
MongoClient.connect('mongodb://localhost/bugsdb', function(err, dbConnection) {
db = dbConnection;
let server = app.listen(3000, function() {
let port = server.address().port;
console.log("Started server at port", port);
});
});