-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserver.js
152 lines (134 loc) · 4.05 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
const express = require('express');
const app = express();
const db = require('./models');
const bodyParser = require('body-parser');
const path = require("path");
app.use(express.static(path.resolve(__dirname, './react-ui/build')));
app.use(bodyParser.urlencoded({
extended : true
}));
////////////////////
////Routes
////////////////////
// **** QUESTIONS ****
// Get one question
app.get('/api/question/:id', function(req, res) {
db.Question.findOne({_id: req.params.id}, function(err, question) {
if (err) {
console.log('Question findOne error in server.js', err);
}
console.log('your single question is ', question);
res.send(question);
})
})
//Get all questions
app.get('/api/question', function(req, res) {
db.Question.find(function(err, question) {
if (err) {
console.log('Question findOne error in server.js', err);
}
console.log('your single question is ', question);
res.send(question);
})
})
// Create question
app.post('/api/question', function (req, res) {
console.log(req.body)
// create new question with form data
var newQuestion = new db.Question({
question: req.body.question,
subject: req.body.subject,
difficulty: req.body.difficulty,
correct: req.body.correct,
incorrect: req.body.incorrect
});
//save the question to the db
newQuestion.save(function(err, question){
if (err) {
res.send(err)
console.log("save question error: " + err);
}
res.send(question)
console.log("saved ", question.question);
})
})
// **** BOARD ****
// Get one board space
app.get('/api/board/:id', function(req, res) {
db.Board.findOne({_id: boardId}, function(err, board) {
if (err) {
console.log('Board findOne error in server.js', err);
}
console.log('your single board space is ', board);
res.send(board);
})
})
// **** USER ****
// Get user data
app.get('/api/user/:id', function(req, res) {
db.User.findOne({_id: userId}, function(err, user) {
if (err) {
console.log('User findOne error in server.js', err);
}
console.log('your single user space is ', user);
res.send(user);
})
})
// Create user
app.post('/api/user', function (req, res) {
console.log(req.body)
// create new user with form data
var newUser = new db.User({
name: req.body.name,
position: req.body.position,
tissues: req.body.tissues,
boogies: req.body.boogies,
correct: req.body.correct,
incorrect: req.body.incorrect
});
//save the user to the db
newUser.save(function(err, user){
if (err) {
return console.log("save user error: " + err);
}
console.log("saved ", user.user.name);
})
})
// edit one user
app.put('/api/user/:id', function (req, res){
var userId = req.params.id;
db.User.findOne({ _id: userId }, function(err, foundUser){
if(err){
console.log('FindOne user error in server.js when trying to edit user', err);
}
console.log('your single user is ');
foundUser.name = req.body.name || foundUser.name;
foundUser.position = req.body.position || foundUser.position;
foundUser.tissues = req.body.tissues || foundUser.tissues;
foundUser.boogies = req.body.boogies || foundUser.boogies;
foundUser.correct = req.body.correct || foundUser.correct;
foundUser.incorrect = req.body.incorrect || foundUser.incorrect;
foundUser.save(function(err, user){
if (err) {
return console.log("save user error: " + err);
}
console.log("saved ", user);
res.json(user);
});
});
});
// delete user
app.delete('/api/user/:id', function (req, res) {
console.log('user delete', req.params);
var userId = req.params.id;
db.User.findOneAndRemove({ _id: userId }, function (err, deletedUser) {
res.json(deletedUser);
});
})
app.get('*', (req, res) => {
console.log(path.resolve(__dirname, './react-ui/build', 'index.html'))
res.sendFile(path.resolve(__dirname, './react-ui/build', 'index.html'));
});
app.listen(process.env.PORT || 3000, function(){
console.log('express server online on port', 3000)
});