-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathapp.js
107 lines (92 loc) · 3.07 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
const { MongoClient } = require('mongodb');
const{ ObjectId } = require('mongodb').ObjectId;
const express = require('express');
const path = require('path');
const bodyParser = require("body-parser");
const app = express();
const port = 3000;
async function main() {
/**
* Connection URI. Update <username>, <password>, and <your-cluster-url> to reflect your cluster.
* See https://docs.mongodb.com/ecosystem/drivers/node/ for more details
*/
const uri = "mongodb://127.0.0.1:27017/eventsList";
const client = new MongoClient(uri, {
useUnifiedTopology: true,
useNewUrlParser: true,
});
try {
// Connect to the MongoDB cluster
await client.connect();
// Make the appropriate DB calls
await init(client);
} catch (e) {
console.error(e);
}
}
main().catch(console.err);
async function init(client) {
app.use(express.static(path.join(__dirname, 'public')));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true })); //extended:true to encode objects and arrays https://github.com/expressjs/body-parser#bodyparserurlencodedoptions
const db = client.db('eventList')
const events = db.collection('events')
app.get('/init', function (req, res) {
events.insertOne({
text: "Some Helpful event",
start_date: new Date(2018, 8, 1),
end_date: new Date(2018, 8, 5)
})
events.insertOne({
text: "Another Cool Event",
start_date: new Date(2018, 8, 11),
end_date: new Date(2018, 8, 11)
})
events.insertOne({
text: "Super Activity",
start_date: new Date(2018, 8, 9),
end_date: new Date(2018, 8, 10)
})
res.send("Test events were added to the database")
});
app.get('/data', function (req, res) {
events.find().toArray(function (err, data) {
//set the id property for all client records to the database records, which are stored in ._id field
for (var i = 0; i < data.length; i++){
data[i].id = data[i]._id;
delete data[i]["!nativeeditor_status"];
}
//output response
res.send(data);
});
});
// Routes HTTP POST requests to the specified path with the specified callback functions. For more information, see the routing guide.
// http://expressjs.com/en/guide/routing.html
app.post('/data', function (req, res) {
var data = req.body;
var mode = data["!nativeeditor_status"];
var sid = data.id;
var tid = sid;
function update_response(err) {
if (err)
mode = "error";
else if (mode == "inserted"){
tid = data._id;
}
res.setHeader("Content-Type", "application/json");
res.send({ action: mode, sid: sid, tid: String(tid) });
}
if (mode == "updated") {
events.updateOne({"_id": ObjectId(tid)}, {$set: data}, update_response);
} else if (mode == "inserted") {
events.insertOne(data, update_response);
} else if (mode == "deleted") {
events.deleteOne({"_id": ObjectId(tid)}, update_response)
} else
res.send("Not supported operation");
});
};
// Binds listens for connections on the specified host and port. This method is identical to Node’s http.Server.listen().
app.listen(port, () => {
console.log("Server is running on port " + port + "...");
});