-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
289 lines (237 loc) · 8.28 KB
/
app.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
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
/*-------------------------------*/
// INITIALIZE EXPRESS
/*-------------------------------*/
var express = require("express"),
mongoose = require("mongoose"),
bodyParser = require("body-parser"),
expressSanitizer = require("express-sanitizer"),
methodOverride = require('method-override');
var app = express();
/*-------------------------------*/
// APP CONFIG
/*-------------------------------*/
/*override with POST having ?_method=PUT OR ?_method=DELETE*/
app.use(methodOverride('_method'));
/* Tell express to serve the contents of the public directory */
app.use(express.static("public"));
/* Template Engine*/
/* EJS - Tell express the file extension of your view/template engine so
you don’t have to type .ejs all the time*/
app.set("view engine", "ejs");
/* body-parser - for html-forms post routes*/
app.use(bodyParser.urlencoded({
extended: true
}));
/*SANITIZE - must go after body parser*/
app.use(expressSanitizer());
/*-------------------------------*/
// DATABASE
/*-------------------------------*/
// Build the connection string
//URI (Uniform Resource Identifier)
var dbURI = "mongodb://localhost/blogapp";
//create the database connection
mongoose.connect(dbURI);
/****** CONNECTION EVENTS
* Remember these are event handers - asynch...
*
*/
// When successfully connected...
mongoose.connection.on("connected", function() {
console.log('Mongoose default connection open to ' + dbURI);
});
// If the connection throws an error
mongoose.connection.on("error", function(err) {
console.log('Mongoose default connection error: ' + err);
});
// When the connection is disconnected
mongoose.connection.on("disconnected", function() {
console.log('Mongoose default connection disconnected');
});
/*Define the SCHEMA*/
/*Interesting: This is how you default a column */
var blogSchema = mongoose.Schema({
title: String,
image: String,
body: String,
created: {
type: Date,
default: Date.now
}
});
/*Build The Model*/
var Blog = mongoose.model("Blog", blogSchema);
/*Temporary Data Setup*/
var createTestData = false;
if (createTestData) {
Blog.remove({}, function(err) {
if (err) {
console.log("Error Removing test Data: " + err);
}
else {
console.log("Removed All Test Data...");
}
});
var blogTestData = [{
title: "Hard at Work",
image: "http://image.shutterstock.com/z/stock-vector-professional-programmer-working-writing-code-at-his-big-desk-with-multiple-displays-and-laptop-390754486.jpg",
body: "I have been doing the career 2.0 thing for a long time Lorem ipsum dolor sit amet, consectetur adipisicing elit. Voluptate, maxime, animi aperiam quis sunt error similique inventore impedit officiis placeat cupiditate eaque cumque aliquam delectus quibusdam nemo molestiae accusamus eveniet.",
}, {
title: "Would Rather be windsurfing",
image: "http://www.sail-world.com/photos/sail-world/photos_2013_1/Alt_DAY%202%20WEBSIZE7.jpg",
body: "<h1>Sigh...</h1>I would love to do this every day",
}, {
title: "Moving to Merritt Island",
image: "https://s-media-cache-ak0.pinimg.com/736x/25/97/d2/2597d2247ab01f4728747550930b2434.jpg",
body: "Some Day <strong>Soon</strong> we will live here",
}];
Blog.create(blogTestData, function(err, testBlogs) {
if (err) {
console.log("Error creating New Data...");
console.log(err);
}
else {
console.log("SUCCESS creating New Data...");
console.log(testBlogs);
}
});
} //if (createTestData)
/*-------------------------------*/
// SETUP ROUTES
/*-------------------------------*/
/*
Name | Path/Url | HTTP Verb | Mongoose Method
-------------------------------------------------
[List all things]
Index /things GET Thing.find()
-------------------------------------------------
[ new thing form]
New /things/new GET N/A
-------------------------------------------------
[Create a new thing, then redirect somewhere]
Create /things POST Thing.create()
-------------------------------------------------
[Show info about one specific thing]
Show /things/:id GET Thing.findById()
-------------------------------------------------
[Show edit form for one thing]
Edit /things/:id/edit GET Thing.findById()
-------------------------------------------------
[Update particular thing, then redirect somewhere]
Update /things/:id PUT Thing.findByIdAndUpdate()
-------------------------------------------------
[Delete a particular thing, then redirect somewhere]
Destroy /things/:id DELETE Thing.findByIdAndRemove()
*/
//INDEX ROUTE-----------------------------------
app.get("/blogs", function(req, res) {
Blog.find({},function(err,blogs) {
if (err) {
console.log("Error in INDEX route: " + err);
} else {
res.render("index",{ blogs: blogs });
}
});
});
/* conventional for the root page to go to the index */
app.get("/", function(req,res){
res.redirect("/blogs");
});
//NEW ROUTE-------------------------------------
app.get("/blogs/new",function(req, res) {
res.render('new');
});
//CREATE ROUTE-------------------------------------
app.post("/blogs",function (req,res) {
//dont forget this ties into body-parser
req.body.blog.body = req.sanitize(req.body.blog.body);
Blog.create(req.body.blog,function (err,newBlog) {
if(err){
console.log(err);
res.render("new");
}else{
console.log("Added new Blog Entry: " + newBlog);
res.redirect("/blogs");
}
});
});
/* SHOW ROUTE
[Show info about one specific thing]
Show /things/:id GET Thing.findById()
*/
app.get("/blogs/:id",function(req, res) {
var blogId = req.params.id;
Blog.findById(req.params.id,function(err,foundBlog){
if(err){
var errMsg = "Error in SHOW ROUTE: " + err;
console.log(errMsg);
res.send(errMsg);
}else{
//console.log("Show Route: " + foundBlog._id);
console.log("Show Route: " + req.params.id);
res.render("show",{blog: foundBlog});
}
});
});
//EDIT ROUTE
app.get("/blogs/:id/edit",function(req, res) {
var blogId = req.params.id;
Blog.findById(blogId,(function(err,foundBlog) {
if(err){
var errMsg = "Error in edit Route: " + err;
console.log(errMsg);
res.send(errMsg);
}else{
console.log("Edit Route: " + foundBlog._id);
//console.log("Edit Route: " + req.params.id);
res.render("edit",{blog: foundBlog});
}
}));
}) ;
/* PUT ROUTE
[Update particular thing, then redirect somewhere]
Update /things/:id PUT Thing.findByIdAndUpdate()
*/
app.put("/blogs/:id",function(req,res) {
//console.log(req.body.blog.body);
req.body.blog.body = req.sanitize(req.body.blog.body);
//console.log(req.body.blog.body);
Blog.findByIdAndUpdate(req.params.id, req.body.blog, function(err,blogToUpdate) {
if(err){
var errMsg = "Error in PUT Route: " + err;
console.log(errMsg);
//res.redirect("/blogs");
res.send(errMsg);
}else{
console.log("PUT Route: " + blogToUpdate._id);
res.redirect("/blogs/" + req.params.id);
}
});
});
/*DELETE Route
[Delete a particular thing, then redirect somewhere]
Destroy /things/:id DELETE Thing.findByIdAndRemove()
*/
app.delete('/blogs/:id',function(req,res) {
//res.send("Welcome to the Delete route...");
Blog.findByIdAndRemove(req.params.id,function(err){
if (err) {
var errMsg = "Error in DELETE Route: " + err;
console.log(errMsg);
res.send(errMsg);
} else {
console.log("DELETE Route: " + req.params.id);
res.redirect("/");
}
});
});
/*-------------------------------*/
// INITIALIZE SERVER
/*-------------------------------*/
/* Cloud9 specific settings
https://udemy-webdevbootcamp-dderocher.c9users.io
process.env.PORT, process.env.IP,
*/
app.listen(process.env.PORT, process.env.IP, function() {
console.log("The Blog APP server has started...");
});