-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
184 lines (149 loc) · 4.77 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
var express = require('express');
var app = express();
var bodyParser = require('body-parser');
var mongoose = require('mongoose');
mongoose.Promise = global.Promise;
mongoose.connect('mongodb://localhost:27017/recipes');
var Recipe = require('./app/models/recipes');
// configure app to use bodyParser()
// this will let us get the data from a POST
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.use(function(req, res, next) {
res.header('Access-Control-Allow-Origin', "*");
res.header('Access-Control-Allow-Methods','GET,PUT,POST,DELETE');
res.header('Access-Control-Allow-Headers', 'Content-Type');
next();
})
var port = process.env.PORT || 8080; // set our port
// Routes for api
var router = express.Router(); // get an instance of the express Router
// middleware to use for all requests
router.use(function(req, res, next) {
// do logging
console.log('API request handled.');
next(); // make sure we go to the next routes and don't stop here
});
// test route to make sure everything is working (accessed at GET http://localhost:8080/api)
router.get('/', function(req, res) {
res.json({ message: 'hooray! welcome to our api!' });
});
// on routes that end in /recipes
router.route('/recipes')
// create a recipe (accessed at POST http://localhost:8080/api/recipes)
.post(function(req, res) {
var recipe = new Recipe(); // create a new instance of the Recipes model
recipe.name = req.body.name; // set the recipes name (comes from the request)
recipe.ingredients = req.body.ingredients;
recipe.description = req.body.description;
recipe.tags = req.body.tags;
recipe.comments = req.body.comments;
recipe.date = req.body.date;
// save the recipe and check for errors
recipe.save(function(err) {
if (err) {
res.send(err);
}
res.json({ message: 'Recipes created!' });
});
})
// get all the recipes (accessed at GET http://localhost:8080/api/recipes)
.get(function(req, res) {
Recipe.find(function(err, recipes) {
if (err) {
res.send(err);
}
res.send(recipes);
});
});
// on routes that end in /recipes/:recipe_id
router.route('/recipes/:recipe_id')
// get the recipe with that id (accessed at GET http://localhost:8080/api/recipes/:recipe_id)
.get(function(req, res) {
Recipe.findById(req.params.recipe_id, function(err, recipe) {
if (err) {
res.send(err);
}
res.json(recipe);
});
})
// update the recipe with this id (accessed at PUT http://localhost:8080/api/recipes/:recipe_id)
.put(function(req, res) {
// use our recipe model to find the recipe we want
Recipe.findById(req.params.recipe_id, function(err, recipe) {
if (err) {
res.send(err);
}
// update the recipe info
// [TODO]
// save the recipe
recipe.save(function(err) {
if (err) {
res.send(err);
}
});
});
})
// delete the recipe with this id (accessed at DELETE http://localhost:8080/api/recipes/:recipe_id)
.delete(function(req, res) {
Recipe.remove({
_id: req.params.recipe_id
}, function(err, recipe) {
if (err)
res.send(err);
res.json({ message: 'Successfully deleted' });
});
});
router.route('/recipes/comment/:recipe_id')
// Add a comment with this id (accessed at PUT http://localhost:8080/api/recipes/comment/:recipe_id)
.put(function (req, res) {
Recipe.findById(req.params.recipe_id, function(err, recipe) {
if (err) {
res.send(err);
}
// update the comments array
var comments = recipe.comments;
var newComment = {"poster": req.body.name, "comment": req.body.comment, "replies": []};
comments.unshift(newComment);
recipe.comments = comments;
// save the recipe
recipe.save(function(err) {
if (err) {
res.send(err);
}
res.json(recipe);
});
});
});
router.route('/recipes/:recipe_id/:comment_id')
// Add a reply with this id (accessed at PUT http://localhost:8080/api/recipes/:recipe_id/:comment_id)
.put(function (req, res) {
Recipe.findById(req.params.recipe_id, function(err, recipe) {
if (err) {
res.send(err);
}
// update the comments array
var comments = recipe.comments;
for (var i = 0; i < comments.length; i++) {
if (comments[i]._id == req.params.comment_id) {
var replies = comments[i].replies;
var newReply = {"poster": req.body.name, "reply": req.body.reply};
replies.unshift(newReply);
comments[i].replies = replies;
recipe.comments = comments;
}
}
// save the recipe
recipe.save(function(err) {
if (err) {
res.send(err);
}
res.json(recipe);
});
});
});
// all routes will be prefixed with /api
app.use('/api', router);
// START THE SERVER
app.listen(port);
console.log('Started listening on port ' + port);