-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
142 lines (132 loc) · 3.39 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
const express = require('express');
const app = express();
const router = express.Router();
const port = process.env.PORT || 8080;
const mongoose = require('mongoose');
const Character = require('./models/character.js');
const DBURL = process.env.MONGODB_URI || 'mongodb://localhost/buffyverse'
mongoose.connect(DBURL);
// bodyparser is another type of middleware for dealing with requests before they get to the person making the request
const bodyParser = require('body-parser');
// i am using the public directory as the location for static files
// .use runs middleware defined above, e.d. bodyparser
app.use(express.static('public'));
app.use(bodyParser.json());
router.route('/')
.get((req, res) => {
res.send({
affirmation: "I may be dead, but I’m still pretty. - Buffy 🔥",
information: "Go to these endpoints to get the info you need:",
characters: "/api/characters"
});
});
// setting up the character endpoint
router.route('/characters')
.get((req,res) => {
console.log('testing get request for char endpoint');
//get all the characters
const character = Character.find();
const query = req.query;
// display the characters alphabetically by their names (name: 1), then their fullnames during GET request (fullname: 1). -1 sorts them reverse alphabetically.
if(query.order_by = 'name') {
character.sort({
name: 1,
fullname: 1
});
}
character.exec({}, (err, docs) => {
//error handling
if (err !== null) {
res
.status(400)
.send({
error: err
});
return;
}
res
.status(200)
.send(docs);
});
})
.post((req, res) => {
const body = req.body;
const character = new Character(body);
// saves this to the db, like insert(). save() is part of the mongoose API
character.save((err, doc) => {
//error handling
if (err !== null) {
res.status(400)
.send({
error: err
});
return;
}
//success handling
res
.status(200)
// consider sending back more info about what should be shown to the user as a response. see the JSON recommendations
.send(doc);
});
console.log(body);
// if we don't call .end or .send(), the request will never end and it will crash the server. .send() is called above, so no need for "res.end()"
})
// doing actions on particular characters
router.route('/characters/:character_id')
.get((req, res) => {
Character.findById(req.params.character_id, (err, character) => {
//error handling
if (err) {
res
.status(400)
.send(err);
return;
}
//success handling
res
.status(200)
.send(character);
});
})
// .put((req, res) => {
// Character.findById(req.params.character_id, (err, character) => {
// // error handling
// if (err) {
// res
// .status(400)
// .send(err);
// return;
// }
// character.save((err, savedCharacter) => {
// if (err !== null) {
// res.status(400)
// .send({
// error: err
// });
// return;
// }
// res
// .status(200)
// .send(character);
// });
// });
// })
.delete((req, res) => {
Character.findByIdAndRemove(req.params.character_id, (err, character) => {
//error handling
if (err) {
res
.status(400)
.send(err)
return;
}
//success handling
res
.status(200)
.send({
message: "Successfully deleted"
});
});
});
app.use('/api', router);
app.listen(port);