-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimonApp.js
112 lines (100 loc) · 2.94 KB
/
simonApp.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
"use strict";
const express = require('express');
const app = express();
const bodyParser = require('body-parser');
const MongoClient = require('mongodb').MongoClient;
const keys = require('./config/keys');
const http = require('http');
let db;
app.use(bodyParser.urlencoded({extended: true}));
app.use(bodyParser.json());
app.use(express.static('public'));
app.set('view engine', 'ejs');
const PORT = process.env.PORT || 5000;
MongoClient.connect(keys.mongoDB, (err, database) => {
if (err) console.log(err);
db = database;
//server start
app.listen(PORT, function() {
console.log('listening on: ' + PORT);
});
});
app.get('/', function(req, res){
//Page Views
db.collection('stats').findOneAndUpdate(
{ name : "Simon_Views" },
{ $inc : { "Total_Views" : 1 } },
{ upsert: true },
(err, result) => {
if (err) console.log(err);
});
//Retrieve Scores to render
db.collection('scores').find().sort({"score":-1}).toArray((err, results) => {
if (err) console.log(err);
//console.log(results);
results.map((e)=>e.toString());
res.render('simon.ejs', {score: results});
});
});
app.post("/score", (req, res) => {
let setDate = new Date();
let date = setDate.toString().substr(4,11);
req.body.time = date;
req.body.score = parseInt(req.body.score);
console.log(req.body);
db.collection('scores').save(req.body, (err, result) => {
if (err) console.log(err);
console.log('Saved to DB');
res.redirect('/');
});
});
app.put('/scores', (req, res) => {
req.body.score = parseInt(req.body.score);
console.log(req.body);
db.collection('scores')
.findOneAndUpdate({name: req.body.name},{
$set: {
score: req.body.score
}
},
{
sort: {_id:-1},
upsert: true
},
(err, result) => {
if (err) return res.send(err);
res.send(result);
});
});
app.delete('/scores', (req, res) => {
console.log('Deleted ' + req.body.name);
db.collection('scores').findOneAndDelete(
{name: req.body.name},
{sort: {_id: -1} },
function(err, result){
if (err) return res.send(500, err);
//what other types of things can we send as the result?
res.send({message: "\"" + req.body.name + "\"" + ' Entry has been deleted'});
});
});
app.get('/db', function(req, res){
db.collection('scores').find().toArray((err, results) => {
if (err) console.log(err);
//console.log(results);
res.render('updateDB.ejs', {score: results});
});
});
app.use(express.static('scripts'));
app.get('/calculator', function(req, res){
res.render('calculator.ejs');
});
app.put('/stats', function(req, res){
db.collection('stats').findOneAndUpdate(
{ name : "Simon_Plays" },
{ $inc : { "Total_Plays" : 1 } },
{ upsert: true },
(err, result) => {
if (err) console.log(err);
}
);
});