-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
116 lines (98 loc) · 3.41 KB
/
Copy pathindex.js
File metadata and controls
116 lines (98 loc) · 3.41 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
const express = require("express") // our express server
const app = express() // generate an app object
const bodyParser = require("body-parser") // requiring the body-parser
const PORT = process.env.PORT || 3001 // port that the server is running on => localhost:3001
const db = require("./models/")
const path = require('path');
const cors = require("cors")
const { connect } = require("mongoose")
app.use(cors())
app.use(bodyParser.json()) // telling the app that we are going to use json to handle incoming payload
function success(res, payload) {
return res.status(200).json(payload)
}
//get all
//or from specified author /questions?authorID=1234
app.get("/questions", async (req, res, next) => {
try {
const authorID = req.query.authorID
if (!authorID){
const teams = await db.Question.find({})
return success(res, teams)
}else{
const teams = await db.Question.find({ authorID: authorID})
return success(res, teams)
}
} catch (err) {
next({ status: 400, message: err })
}
})
//answers to question id
app.get("/questions/:id/answers", async (req, res, next) => {
try {
let questionID = req.params.id
const teams = await db.Answer.find({ questionID: questionID})
return success(res, teams)
} catch (err) {
next({ status: 400, message: err })
}
})
//answer to question id by author, returns false if author didnt answer
app.get("/questions/:questionID/answers/:authorID", async (req, res, next) => {
try {
const questionID = req.params.questionID
const authorID = req.params.authorID;
const answer = await db.Answer.findOne({ questionID: questionID, authorID: authorID })
const ret = answer != null ? answer : false
return success(res, ret)
} catch (err) {
next({ status: 400, message: err })
}
})
app.delete("/questions/:questionID", async (req, res, next) => {
try {
const questionID = req.params.questionID
const teams = await db.Question.deleteOne({ _id: questionID})
return success(res, teams)
} catch (err) {
next({ status: 400, message: err })
}
})
//////////////create
/////////////////////////////
app.post("/questions", async (req, res, next) => {
try {
const question = await db.Question.create(req.body)
return success(res, question)
} catch (err) {
next({ status: 400, message: "err is " + err })
}
})
//create
app.post("/answers", async (req, res, next) => {
try {
const answer = await db.Answer.create(req.body)
return success(res, answer)
} catch (err) {
next({ status: 400, message: err })
}
})
//send frontend
app.use(express.static('./frontend/build'));
// If no API routes are hit, send the React app, this doenst work but doesnt seem neccesary?
app.use(function (req, res) {
const uid = path.join(__dirname, './frontend/build/index.html')
res.sendFile(uid);
});
//idk if this can still be reached with the above
app.use((err, req, res, next) => {
console.log("other: " + err.status + " " + err.message)
return res.status(err.status || 400).json({
status: err.status || 400,
message: err.message || "there was an error processing request",
})
})
app.listen(PORT, () => {
// listening on port 3000
console.log(`listening on port ${PORT}`) // print this when the server starts
})