-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathserver.js
More file actions
316 lines (260 loc) · 7.94 KB
/
server.js
File metadata and controls
316 lines (260 loc) · 7.94 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
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
// require express and other modules
var express = require('express'),
app = express(),
cors = require('cors'),
bodyParser = require('body-parser'),
mongoose = require('mongoose');
// configure cors (for allowing cross-origin requests)
app.use(cors());
// configure bodyParser (for receiving form data)
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
// serve static files from public folder
app.use(express.static(__dirname + '/public'));
// set view engine to ejs
app.set('view engine', 'ejs');
// connect to mongodb
mongoose.connect(
process.env.MONGODB_URI ||
process.env.MONGOHQ_URL ||
'mongodb://localhost/mutably'
);
// require models and seed data
var Book = require('./models/book'),
Pokemon = require('./models/pokemon'),
Album = require('./models/album'),
seedBooks = require('./seeds/books'),
seedPokemon = require('./seeds/pokemon'),
seedAlbum = require('./seeds/album');
/*
* Get a single response object and 404 if it isn't found.
* @param err {Object} Error reported from Mongo.
* @param foundObject {Object} An Object found by Mongo.
* @this Express Response Object
*/
function getSingularResponse (err, foundObject) {
if (err) {
this.status(500).json({ error: err.message });
} else {
if (foundObject === null) {
this.status(404).json({ error: "Nothing found by this ID." });
} else {
this.status(200).json(foundObject);
}
}
}
// API ROUTES
/*
* BOOK API ENDPOINTS
*/
// get all books
app.get('/books', function (req, res) {
// find all books in db
Book.find(function (err, allBooks) {
if (err) {
res.status(500).json({ error: err.message });
} else {
res.json({ books: allBooks });
}
});
});
// create new book
app.post('/books', function (req, res) {
// create new book with form data (`req.body`)
var newBook = new Book(req.body);
// save new book in db
newBook.save(function (err, savedBook) {
if (err) {
res.status(500).json({ error: err.message });
} else {
res.status(201).json(savedBook);
}
});
});
app.get('/books/:id', function (req, res) {
// get book id from url params (`req.params`)
var bookId = req.params.id;
// find book in db by id
Book.findOne({ _id: bookId }, getSingularResponse.bind(res));
});
// update book
app.put('/books/:id', function (req, res) {
// get book id from url params (`req.params`)
var bookId = req.params.id;
// find book in db by id
Book.findOne({ _id: bookId }, function (err, foundBook) {
if (err) {
return res.status(500).json({ error: err.message });
}
if (!foundBook){
return res.status(404).json({ error: "Nothing found by this ID." });
}
// update the books's attributes
foundBook.title = req.body.title;
foundBook.author = req.body.author;
foundBook.image = req.body.image;
foundBook.releaseDate = req.body.releaseDate;
// save updated book in db
foundBook.save(getSingularResponse.bind(res));
});
});
// delete book
app.delete('/books/:id', function (req, res) {
// get book id from url params (`req.params`)
var bookId = req.params.id;
// find book in db by id and remove
Book.findOneAndRemove({ _id: bookId }, getSingularResponse.bind(res));
});
/*
* POKEMON API ENDPOINTS
*/
// get all pokemon
app.get('/pokemon', function (req, res) {
// find all pokemon in db
Pokemon.find(function (err, allPokemons) {
if (err) {
res.status(500).json({ error: err.message });
} else {
res.json({ pokemon: allPokemons });
}
});
});
// create new pokemon
app.post('/pokemon', function (req, res) {
// create new pokemon with form data (`req.body`)
var newPokemon = new Pokemon(req.body);
// save new book in db
newPokemon.save(function (err, savedPokemon) {
if (err) {
res.status(500).json({ error: err.message });
} else {
res.status(201).json(savedPokemon);
}
});
});
// get one pokemon
app.get('/pokemon/:id', function (req, res) {
// get pokemon id from url params (`req.params`)
var pokemonId = req.params.id;
// find pokemon in db by id
Pokemon.findOne({ _id: pokemonId }, getSingularResponse.bind(res));
});
// update pokemon
app.put('/pokemon/:id', function (req, res) {
// get pokemon id from url params (`req.params`)
var pokemonId = req.params.id;
// find pokemon in db by id
Pokemon.findOne({ _id: pokemonId }, function (err, foundPokemon) {
if (err) {
return res.status(500).json({ error: err.message });
}
if (!foundPokemon){
return res.status(404).json({ error: "Nothing found by this ID." });
}
// update the pokemon's attributes
foundPokemon.name = req.body.name;
foundPokemon.pokedex = req.body.pokedex;
foundPokemon.evolves_from = req.body.evolves_from;
foundPokemon.image = req.body.image;
// save updated pokemon in db
foundPokemon.save(getSingularResponse.bind(res));
});
});
// delete pokemon
app.delete('/pokemon/:id', function (req, res) {
// get pokemon id from url params (`req.params`)
var pokemonId = req.params.id;
// find pokemon in db by id and remove
Pokemon.findOneAndRemove({ _id: pokemonId }, getSingularResponse.bind(res));
});
/*
* ALBUM API ENDPOINTS
*/
// get all albums
app.get('/albums', function (req, res) {
// find all albums in db
Album.find(function (err, allAlbums) {
if (err) {
res.status(500).json({ error: err.message });
} else {
res.json({ albums: allAlbums });
}
});
});
// create new album
app.post('/albums', function (req, res) {
// create new album with form data (`req.body`)
var newAlbum = new Album(req.body);
// save new album in db
newAlbum.save(function (err, savedAlbum) {
if (err) {
res.status(500).json({ error: err.message });
} else {
res.status(201).json(savedAlbum);
}
});
});
app.get('/albums/:id', function (req, res) {
// get album id from url params (`req.params`)
var albumId = req.params.id;
// find album in db by id
Album.findOne({ _id: albumId }, getSingularResponse.bind(res));
});
// update album
app.put('/albums/:id', function (req, res) {
// get album id from url params (`req.params`)
var albumId = req.params.id;
// find album in db by id
Album.findOne({ _id: albumId }, function (err, foundAlbum) {
if (err) {
return res.status(500).json({ error: err.message });
}
if (!foundAlbum){
return res.status(404).json({ error: "Nothing found by this ID." });
}
// update the albums's attributes
foundAlbum.artistName = req.body.artistName;
foundAlbum.name = req.body.name;
foundAlbum.releaseDate = req.body.releaseDate;
foundAlbum.genres = req.body.genres;
// save updated album in db
foundAlbum.save(getSingularResponse.bind(res));
});
});
// delete album
app.delete('/albums/:id', function (req, res) {
// get album id from url params (`req.params`)
var albumId = req.params.id;
// find album in db by id and remove
Album.findOneAndRemove({ _id: albumId }, getSingularResponse.bind(res));
});
// HOME & RESET ROUTES
app.get('/', function (req, res) {
res.render('site/index');
});
app.get('/reset', function (req, res) {
res.render('site/reset');
});
app.post('/reset', function (req, res) {
Book.remove({}, function (err, removedBooks) {
Book.create(seedBooks, function (err, createdBooks) {
Pokemon.remove({}, function (err, removedPokemons) {
Pokemon.create(seedPokemon, function (err, createdPokemons) {
Album.remove({}, function (err, removedAlbums) {
Album.create(seedAlbum, function (err, createdAlbums) {
if (req.params.format === 'json') {
res.status(201).json(createdBooks.concat(createdWines).concat(createdPokemons));
} else {
res.redirect('/');
}
});
});
});
});
});
});
});
// listen on port (production or localhost)
app.listen(process.env.PORT || 3000, function() {
console.log('server started');
});