1
+ const express = require ( "express" ) ;
2
+ const app = express ( ) ;
3
+ const bodyParser = require ( "body-parser" ) ;
4
+ const mongodb = require ( 'mongodb' ) ;
5
+ const expressSanitizer = require ( "express-sanitizer" ) ;
6
+ const methodOverride = require ( "method-override" ) ;
7
+ const db = require ( "./db" ) ;
8
+ const ObjectId = mongodb . ObjectId ;
9
+
10
+
11
+ app . set ( "view engine" , "ejs" ) ; // Alloy using ejs, without write the complete name
12
+ app . use ( bodyParser . urlencoded ( { extended : true } ) ) ; // it's for gettin the input from the forms
13
+ app . use ( express . static ( "public/stylesheets" ) ) ; // this allow to get the CSS styles inse the folder path
14
+ app . use ( bodyParser . json ( ) ) ;
15
+ app . use ( expressSanitizer ( ) ) ;
16
+ app . use ( methodOverride ( "_method" ) ) ;
17
+
18
+
19
+ const collection = "blogs" ;
20
+
21
+
22
+ //RESTFULL ROUTES
23
+ app . get ( "/" , function ( req , res ) {
24
+ res . redirect ( "/blogs" ) ;
25
+ } ) ;
26
+
27
+
28
+ //INDEX ROUTE
29
+ app . get ( "/blogs" , function ( req , res ) {
30
+ db . getDB ( ) . collection ( collection ) . find ( { } ) . toArray ( ( err , blogs ) => {
31
+ if ( err ) {
32
+ console . log ( "ERROR" ) ;
33
+ } else {
34
+ res . render ( "index" , { blogs : blogs } ) ;
35
+ }
36
+ } ) ;
37
+
38
+ } ) ;
39
+
40
+ //NEW ROUTE
41
+ app . get ( "/blogs/new" , function ( req , res ) {
42
+ res . render ( "new" ) ;
43
+ } ) ;
44
+
45
+ //CREATE ROUTE
46
+ app . post ( "/blogs" , function ( req , res ) {
47
+ //create blog
48
+ let title = req . body . title ;
49
+ let image = req . body . image ;
50
+ let content = req . sanitize ( req . body . content ) ;
51
+ let createdat = Date ( "<YYYY-mm-ddTHH:MM:ss>" ) ;
52
+ let newBlog = { title : title , image : image , content : content , createdat : createdat } ;
53
+ console . log ( newBlog ) ;
54
+ console . log ( "=======================================" ) ;
55
+ db . getDB ( ) . collection ( collection ) . insertOne ( newBlog , function ( err , newBlog ) {
56
+ if ( err ) {
57
+ res . render ( "new" ) ;
58
+ } else {
59
+ //then reditect to blogs
60
+ //console.log(newBlog);
61
+ res . redirect ( "/blogs" ) ;
62
+ }
63
+ } ) ;
64
+ } ) ;
65
+
66
+
67
+ //SHOW ROUTE
68
+ app . get ( "/blogs/:id" , function ( req , res ) {
69
+ const blogID = req . params . id ;
70
+
71
+ db . getDB ( ) . collection ( collection ) . findOne ( { _id : new ObjectId ( blogID ) } , function ( err , foundBlog ) {
72
+ if ( err ) {
73
+ res . redirect ( "/blogs" ) ;
74
+ } else {
75
+ console . log ( foundBlog )
76
+ res . render ( "show" , { blog : foundBlog } ) ;
77
+ }
78
+ } ) ;
79
+ } ) ;
80
+
81
+
82
+ //EDIT ROUTE
83
+ app . get ( "/blogs/:id/edit" , function ( req , res ) {
84
+ const blogID = req . params . id ;
85
+
86
+ db . getDB ( ) . collection ( collection ) . findOne ( { _id : new ObjectId ( blogID ) } , function ( err , foundBlog ) {
87
+ if ( err ) {
88
+ res . redirect ( "/blogs" ) ;
89
+ } else {
90
+ res . render ( "edit" , { blog : foundBlog } ) ;
91
+ }
92
+ } ) ;
93
+ } ) ;
94
+
95
+
96
+ //UPDATE ROUTE
97
+ app . put ( "/blogs/:id" , function ( req , res ) {
98
+ //create blog
99
+ const blogID = req . params . id ;
100
+ let title = req . body . title ;
101
+ let image = req . body . image ;
102
+ let content = req . sanitize ( req . body . content ) ;
103
+ let createdat = new Date ( "<YYYY-mm-ddTHH:MM:ss>" ) ;
104
+ let updateBlog = { title : title , image : image , content : content , createdat : createdat } ;
105
+
106
+ db . getDB ( ) . collection ( collection ) . updateOne ( { _id : new ObjectId ( blogID ) } , { $set : updateBlog } , function ( err , updateBlog ) {
107
+ if ( err ) {
108
+ res . redirect ( "/blogs" ) ;
109
+ } else {
110
+ res . redirect ( "/blogs/" + req . params . id ) ;
111
+ }
112
+ } ) ;
113
+ } ) ;
114
+
115
+ //DELETE ROUTE
116
+ app . delete ( "/blogs/:id" , function ( req , res ) {
117
+ const blogID = req . params . id ;
118
+
119
+ //destroy blog
120
+ db . getDB ( ) . collection ( collection ) . deleteOne ( { _id : new ObjectId ( blogID ) } , function ( err ) {
121
+ if ( err ) {
122
+ res . redirect ( "/blogs" ) ;
123
+ } else {
124
+ res . redirect ( "/blogs" ) ;
125
+ }
126
+ } ) ;
127
+ //redirect somewhere
128
+ } ) ;
129
+
130
+
131
+
132
+
133
+ /// Establishing the conection of the DB and running the App if everything is OK
134
+ db . connect ( ( err ) => {
135
+ if ( err ) {
136
+ console . log ( 'unable to connect to database Broh' ) ;
137
+ process . exit ( )
138
+ } else {
139
+ app . listen ( 8080 , function ( ) {
140
+ console . log ( "This server is runing in PORT 8080 Broh!, and the DB is also running" )
141
+ } ) ;
142
+ }
143
+ } ) ;
0 commit comments