forked from cs4241-20a/a4-components
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
116 lines (95 loc) · 2.98 KB
/
Copy pathserver.js
File metadata and controls
116 lines (95 loc) · 2.98 KB
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
108
109
110
111
112
113
114
115
116
const express = require( 'express' ),
app = express(),
mongodb = require( 'mongodb' ),
bp = require( 'body-parser')
var data = [
"Patrick Star Freshman 4 2020 B-",
"Spongebob Squarepants Junior 45 2019 F",
"Sandy Cheeks Sophmore 0 2021 A+",
"Plankton Lawrence Senior 4 2020 B+",
"Eugene Krabs Senior 1 2020 C",
"Pearl Krabs Freshman 0 2023 A-",
"Squidward Tentacles Junior 2 2021 C-"
];
const login = [];
function update(){
try{
collection.updateOne({_id: mongodb.ObjectId('5f7281b84146de20dcfa8b91')},
{$set: {data: data}});
}
catch(e){
console.log(e);
}
}
//start the server up
app.use( bp.json() )
app.use( express.static( 'build' ) )
const MongoClient = mongodb.MongoClient;
const uri = "mongodb+srv://rorysully:jiSdvf4Kq5TCxANa@cluster0.btzeq.mongodb.net/<dbname>?retryWrites=true&w=majority";
const client = new MongoClient(uri, { useNewUrlParser: true });
client.connect(err => {
collection = client.db("PuffsBoatingSchool").collection("UserData");
collection.findOne({_id: mongodb.ObjectId('5f7281b84146de20dcfa8b91')}).then(response => {
data = response.data;
});
});
// send the default array of dreams to the webpage
app.get("/dreams", (request, response) => {
// express helps us take JS objects and send them as JSON
response.json(data);
});
app.post("/add", bp.json(), (request, responce) => {
data.push( request.body.dream );
update();
responce.json(request.body);
});
app.post("/login", bp.json(), (request, responce) => {
var returnVal = [...login];
console.log(login.length)
if(login.length == 0){
var res = request.body.dream.split(" ");
var newRes = request.body.dream.split(" ");
login.push(res);
newRes.push("New User");
returnVal.push(newRes);
}
console.log(returnVal);
responce.json(returnVal);
});
app.delete("/remove", (request, responce) => {
for(var i = 0; i < data.length; i++){
var res = data[i].split(" ");
if(res[0] + " " + res[1] == request.body.dream){
if (i > -1) {
data.splice(i, 1);
}
}
}
update();
responce.json(data);
});
app.post("/modify", bp.json(), (request, responce) => {
var result = request.body.dream.split(" : ");
var i;
for(i = 0; i < data.length; i++){
var res = data[i].split(" ");
console.log(res[0] + " " + res[1]);
console.log(res);
if(res[0] + " " + res[1] == result[0]){
if (i > -1) {
console.log("HERE");
data.splice(i, 1);
data.splice(i, 0, result[1]);
}
}
}
update()
responce.json(data);
});
app.get( '/read', ( req, res ) => res.json( todos ) )
app.post( '/change', function( req,res ) {
const idx = todos.findIndex( v => v.name === req.body.name )
todos[ idx ].completed = req.body.completed
res.sendStatus( 200 )
})
app.listen( 8080 )