-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathserver.js
More file actions
118 lines (106 loc) · 3.25 KB
/
Copy pathserver.js
File metadata and controls
118 lines (106 loc) · 3.25 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
117
118
const express = require("express");
const bodyParser = require("body-parser");
const app = express();
const mongoose = require("mongoose");
const path = require("path");
const incidentModel = require("./models/incident");
const PORT = process.env.PORT || 5000;
app.use(bodyParser.json({ limit: "30mb", extended: "true" }));
app.use(bodyParser.urlencoded({ limit: "30mb", extended: "true" }));
const CONNECTION_URL = process.env.DATABASE_CONNECTION_URL;
mongoose
.connect(CONNECTION_URL, { useNewUrlParser: true, useUnifiedTopology: true })
.then(() =>
app.listen(PORT, () => console.log(`Server running on port: ${PORT}`))
)
.catch((error) => console.log(error.message));
mongoose.set("useFindAndModify", false);
//////////////API calls/////////////////
/*Shape of expected reqeust for each end point
-/api/report-incident
- expected data shape
{ "type":"ASSAULT", "date":"2020/12/12", "location": {
"lat": "39.980818972",
"lng": "-76.8469380289"
}}
-/api/update-incident
- expected data shape
{
{
"query": {
"date": "2020-12-12",
"type": "ASSAULT",
"location": {
"lat": "39.980818972",
"lng": "-76.8469380289"
}
},
"update":
{
"date": "2020-12-12",
"type": "MURDER",
"location": {
"lat": "39.980818972",
"lng": "-76.8469380289"
}
}
}
-/api/delete-incident
-expected data shape
{ "type":"ASSAULT", "date":"2020/12/12", "location": {
"lat": "39.980818972",
"lng": "-76.8469380289"
}}
*/
app.get("/api", (req, res) => {
// Sends a basic hello respone if the root endpoint of our api is hit
res.send("Hello, welcome to CrimeSurge's API");
});
app.get("/api/incident-data", async (req, res) => {
//Sends a json representation of all of the reported incidents in our database
const incidents = await incidentModel.find({});
try {
res.json(incidents);
} catch (err) {
res.status(500).send(err);
}
});
app.post("/api/report-incident", async (req, res) => {
//Adds an incident to our database
const incident = new incidentModel(req.body);
try {
await incident.save();
res.send(incident);
} catch (err) {
res.status(500).send(err);
}
});
app.put("/api/update-incident", async (req, res) => {
//updates the details of a speciffic incident with the data provided
const query = req.body["query"];
const update = req.body["update"];
console.log(query, update);
try {
const response = await incidentModel.updateOne(query, update);
res.send(req.body["update"]);
} catch (err) {
res.status(500).send(err, req.body);
}
});
app.delete("/api/delete-incident", async (req, res) => {
//deletes the incident that matches the req.body
try {
const response = await incidentModel.deleteOne(req.body);
res.send(response);
} catch (err) {
res.status(500).send(err, req.body);
}
});
if (process.env.NODE_ENV === "production") {
// Serve any static files
app.use(express.static(path.join(__dirname, "Client/build")));
// Handle React routing, return all requests to React app
app.get("*", function (req, res) {
res.sendFile(path.join(__dirname, "Client/build", "index.html"));
});
}