-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathserver.js
231 lines (189 loc) · 5.7 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
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
// 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.MONGOLAB_URI ||
process.env.MONGOHQ_URL ||
'mongodb://localhost/crud-api'
);
// require models and seed data
var Book = require('./models/book'),
Wine = require('./models/wine'),
seedBooks = require('./seeds/books'),
seedWines = require('./seeds/wines');
// API ROUTES
// 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);
}
});
});
/*
* 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);
}
}
}
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));
});
// get all wines
app.get('/wines', function (req, res) {
// find all wines in db
Wine.find(function (err, allWines) {
if (err) {
res.status(500).json({ error: err.message });
} else {
res.json({ wines: allWines });
}
});
});
// create new wine
app.post('/wines', function (req, res) {
// create new wine with form data (`req.body`)
var newWine = new Wine(req.body);
// save new book in db
newWine.save(function (err, savedWine) {
if (err) {
res.status(500).json({ error: err.message });
} else {
res.status(201).json(savedWine);
}
});
});
// get one wine
app.get('/wines/:id', function (req, res) {
// get wine id from url params (`req.params`)
var wineId = req.params.id;
// find wine in db by id
Wine.findOne({ _id: wineId }, getSingularResponse.bind(res));
});
// update wine
app.put('/wines/:id', function (req, res) {
// get wine id from url params (`req.params`)
var wineId = req.params.id;
// find wine in db by id
Wine.findOne({ _id: wineId }, function (err, foundWine) {
if (err) {
return res.status(500).json({ error: err.message });
}
if (!foundWine){
return res.status(404).json({ error: "Nothing found by this ID." });
}
// update the wine's attributes
foundWine.name = req.body.name;
foundWine.year = req.body.year;
foundWine.country = req.body.country;
foundWine.description = req.body.description;
foundWine.image = req.body.image;
foundWine.price = req.body.price;
// save updated wine in db
foundWine.save(getSingularResponse.bind(res));
});
});
// delete wine
app.delete('/wines/:id', function (req, res) {
// get wine id from url params (`req.params`)
var wineId = req.params.id;
// find wine in db by id and remove
Wine.findOneAndRemove({ _id: wineId }, 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) {
Wine.remove({}, function (err, removedWines) {
Wine.create(seedWines, function (err, createdWines) {
if (req.params.format === 'json') {
res.status(201).json(createdBooks.concat(createdWines));
} else {
res.redirect('/');
}
});
});
});
});
});
// listen on port (production or localhost)
app.listen(process.env.PORT || 3000, function() {
console.log('server started');
});