forked from wtgibson/geo-hunt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
173 lines (147 loc) · 4.15 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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
const express = require("express");
const mongoose = require("mongoose");
const path = require("path");
const PORT = process.env.PORT || 3001;
const app = express();
// const router = require("express").Router();
const Hunt = require("./models/Hunt");
// Define middleware here
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
app.use(express.static("client/build"));
// Serve up static assets (usually on heroku)
if (process.env.NODE_ENV === "production") {
app.use(express.static("client/build"));
app.use(express.static("client/public"));
}
mongoose.connect(process.env.MONGODB_URI || "mongodb://localhost/geohunt", {
useNewUrlParser: true,
useFindAndModify: false
});
// Define API routes here
app.get("/api/hunts", (req, res) => {
Hunt.find({})
.then(hunt => {
res.json(hunt);
})
.catch(err => {
res.status(400).json(err);
});
});
app.get("/api/hunt/:id", (req, res) => {
Hunt.find({
"targetInfo.targetId": req.params.id
})
.then(hunt => {
res.json(hunt);
})
.catch(err => {
res.status(400).json(err);
});
});
app.get("/api/createexpedition/:size", ({ params }, res) => {
// console.log(params.size)
Hunt.find({})
.then(hunt => {
// Randomize and pick 5 and send
const huntArray = Object.keys(hunt).map(i => hunt[i])
for (var i = huntArray.length - 1; i > 0; i--) {
// Random number generated used to swap hunts
var j = Math.floor(Math.random() * (i + 1))
var temp = huntArray[i];
huntArray[i] = huntArray[j];
huntArray[j] = temp
};
var Expedition = []
for (var v = 0; v < params.size; v++) {
// console.log(v)
// Expedition.push(huntArray[v].targetInfo.targetId)
Expedition.push(huntArray[v])
};
// console.log(huntArray)
// console.log(Expedition)
res.json(Expedition);
})
.catch(err => {
res.status(400).json(err);
});
});
app.post("/api/createhunt/", ({ body }, res) => {
console.log(body)
var newHunt = {
targetInfo: {
targetName: body.targetName,
targetId: body.targetId,
targetLat: body.targetLat,
targetLng: body.targetLng,
targetAccuracy: 0.025,
targetCategory: body.targetCategory,
targetLikes: body.targetLikes,
targetAddress: body.targetAddress,
targetCrossStreets: body.targetCrossStreets,
targetNeighborhood: body.targetNeighborhood,
targetFactoid: body.targetFactoid,
targetPhoto: body.targetPhoto,
targetGooglePlacesId: body.targetGooglePlacesId,
targetRating: body.targetRating,
targetVicinity: body.targetVicinity,
targetTypes: body.targetTypes,
targetURL: body.targetURL
},
clues: {
clue1: body.clue1,
clue2: body.clue2,
clue3: body.clue3,
clue4: body.clue4,
clue5: body.clue5,
clue6: body.clue6,
clue7: body.clue7,
clue8: body.clue8,
clue9: body.clue9,
clue10: body.clue10
}
}
Hunt.collection.insertOne(newHunt)
.then(data => {
console.log(data.result.n + " records inserted!");
// process.exit(0);
})
.catch(err => {
console.error(err);
// process.exit(1);
});
});
app.delete("/api/hunt/:id", (req, res) => {
Hunt.deleteOne({
"_id": req.params.id
}).then(() => res.status(200).json(true))
.catch(err => {
res.status(400).json(err);
});
});
// Send every other request to the React app
// Define any API routes before this runs
// hunt is the client, index.html seems to be for react
app.get("/", (req, res) => {
res.sendFile(path.join(__dirname, "./client/build/index.html"));
});
app.get("/hunt", (req, res) => {
res.sendFile(path.join(__dirname, "./client/build/index.html"));
});
app.get("/createhunt", (req, res) => {
res.sendFile(path.join(__dirname, "./client/build/create-hunt.html"));
});
app.get("/api/hunt/:id", (req, res) => {
Hunt.find({
"targetInfo.targetId": req.params.id
})
.then(hunt => {
res.json(hunt);
})
.catch(err => {
res.status(400).json(err);
});
});
app.listen(PORT, () => {
console.log(`API server now on port ${PORT}!`);
});