-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
156 lines (127 loc) · 3.89 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
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
const port = process.env.PORT || 3001;
const express = require('express');
const app = express();
//NOSERVER?
const NOSERVER = process.env.NOSERVER || 0;
if(NOSERVER) console.log("Debugging with NOSERVER="+NOSERVER);
const FAKE_DATABASE = [
{ id: 0, name: "Name1", animal: "Panda" },
{ id: 1, name: "Name2", animal: "Penguin" },
{ id: 2, name: "Name3", animal: "Dolphin"}
];
//Handling MongoDB
const { MongoClient, ObjectId } = require('mongodb');
//Set this yourself
const DB_USERNAME = "mongo";
const DB_NAME = process.env.MONGONAME || "admin";
//Should be automatically setup on npm start; see README > Database Setup for more details
const DB_PASSWORD = process.env.MONGOPASSWORD || "password";
const DB_HOST_URL = process.env.MONGOHOST || "192.168.27.100";
const DB_PORT = process.env.MONGOPORT || "27017";
const MONGODB_URL = "mongodb://"+DB_USERNAME+":"+DB_PASSWORD+"@"+DB_HOST_URL+":"+DB_PORT+"/"+DB_NAME;
//const MONGODB_URL = "mongodb+srv://admin:<password>@pandas-ef9ir.mongodb.net/test?retryWrites=true";
console.log("Connecting to "+DB_HOST_URL+":"+DB_PORT+"/"+DB_NAME+"...");
const client = new MongoClient(MONGODB_URL, { useNewUrlParser: true });
let db;
const COLLECTION_NAME = "votes";
if(!NOSERVER){
client.connect(err => {
if(err) return console.log("ERROR: "+err);
db = client.db("cuddly-animals");
console.log("Connected to MongoDB server at "+MONGODB_URL);
app.listen(port, () => {
console.log("Listening on http://localhost:" + port);
});
});
}
app.set('view engine', 'ejs');
const bodyParser= require('body-parser');
app.use(bodyParser.urlencoded({extended: true}));
app.use(bodyParser.json());
app.use(express.static('public'));
//Handling index.html and subsequent votes
app.get('/', (req, res) => {
console.log("Retrieving from collection "+COLLECTION_NAME+"...");
if(NOSERVER){
// send HTML file populated with quotes here
res.render('index.ejs', {
"votes": FAKE_DATABASE
});
return;
}
if(!db){
res.sendFile(__dirname + '/index.html'); //note, path must be absolute
console.log("ERROR: MongoDB is not connected.");
return;
}
db.collection(COLLECTION_NAME).find().toArray((err, results) => {
if(err){
res.sendFile(__dirname + '/index.html'); //note, path must be absolute
return console.log("ERROR: "+err);
}
console.log("Results: ",JSON.stringify(results, null, 2))
// send HTML file populated with quotes here
res.render('index.ejs', {
"votes": results
})
});
});
//Handling forms
app.post('/votes_form', (req, res)=>{
if(!req.body.name.length || !req.body.animal.length){
res.redirect('/');
return;
}
db.collection(COLLECTION_NAME).insertOne(req.body, (err, result) => {
if(err) return console.log("ERROR: "+err);
console.log('Saved to database: '+result);
res.redirect('/');
})
})
app.put('/votes_form', (req, res) => {
//console.log("Panda Invasion!", req, res);
console.log(req.body);
db.collection(COLLECTION_NAME).findOneAndUpdate({
_id: ObjectId(req.body._id)
}, {
$set: {
name: req.body.name,
animal: req.body.animal
}
}, {
//sort: {_id: -1},
//upsert: true,
returnOriginal: false
}, (err, result) => {
if (err) return res.send(err);
res.send(result);
})
})
app.delete('/votes_form', (req, res) => {
db.collection(COLLECTION_NAME).findOneAndDelete({
_id: ObjectId(req.body._id)
},
(err, result) => {
if (err) return res.send(500, err)
res.send({
_id: req.body._id,
subject: 'delete',
message: 'Deleted vote with id '+req.body.id
})
})
})
// NOSERVER
if(NOSERVER){
app.listen(port, () => {
console.log("Listening on http://localhost:" + port);
});
}
//Handling 404 not found
// 404
app.use(function(req, res, next) {
return res.status(404).sendFile(__dirname + '/public/404.html'); //note, path must be absolute
});
// 500 - Any server error
app.use(function(err, req, res, next) {
return res.status(404).sendFile(__dirname + '/public/500.html'); //note, path must be absolute
});