forked from TheUsualSubsets/mApper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
99 lines (76 loc) · 2.47 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
var express = require('express');
var request = require('request');
var mongo = require('mongodb');
var mongoose = require('mongoose');
var bodyParser = require('body-parser');
var query = require('./server/dbqueries.js');
var app = express();
app.use(bodyParser.json());
var PORT = process.env.PORT || 9001;
app.use(express.static(__dirname + '/client'));
//------------------------------MAIN ROUTES--------------------------//
app.get('/newGame', function(req, res){
console.log('starting');
//check if there was a querystring (from a shared link) attached to the req
if(req.query.q){
//if there was, query DB based on that querystring
query.challengeQuery(req.query.q, function(result){
res.send(result);
})
//if not, return a random location
} else {
query.randomQuery(function(result) {
res.send(result);
});
}
});
app.post('/api/addPoint', function(req, res) {
//add the new point to the database
query.addToDatabase(req.body, function(err, newEntry){
if(err){
console.error(err);
}
//the above query returns the newly-added point
//extract the unique ID and create a url using the ID as a querystring
var ID = newEntry._id;
// var testIP = 'http://localhost:9001/';
var IP = 'http://www.parachute9001.com/'
// console.log(IP + 'newGame?q=' + ID)
res.send(IP + '#/game?q=' + ID);
// res.send(IP + 'newGame?q=' + ID);
});
})
//---------------------------DB MAINTANANCE--------------------------//
//the following functions are used for maintaining/monitoring the database only
//they are not used in rendering the site or for gameplay
//for easily obtaining all entries in the DB via Postman
app.get('/api/getAll', function(req, res) {
query.getAllQuery(req.query.city, function(results) {
res.send(results);
})
})
//for updating incorrect DB entries
app.post('/api/update', function(req, res) {
query.updateEntry(req.body.lookup, req.body.update, function(results) {
res.send(results);
})
})
//sends back a list of unique cities in the database ;)
app.get('/api/distinct', function(req, res) {
query.distinctQuery(function(results) {
res.send(results);
})
})
app.get('/scores', function(req, res) {
query.getScores(function(results) {
res.send(200, results);
})
})
app.post('/scores', function(req, res) {
query.addScores(req.body, function(results) {
res.send(200);
});
})
app.listen(PORT, function(){
console.log('listening on PORT ' + PORT);
})