@@ -10,54 +10,59 @@ var express = require('express'),
10
10
users : db . collection ( 'users' )
11
11
} ;
12
12
13
+ // Express.js Middleware
14
+ var session = require ( 'express-session' ) ,
15
+ logger = require ( 'morgan' ) ,
16
+ errorHandler = require ( 'errorhandler' ) ,
17
+ cookieParser = require ( 'cookie-parser' ) ,
18
+ bodyParser = require ( 'body-parser' ) ,
19
+ methodOverride = require ( 'method-override' ) ;
13
20
14
21
var app = express ( ) ;
15
- app . locals . appTitle = " blog-express" ;
22
+ app . locals . appTitle = ' blog-express' ;
16
23
24
+ // Expose collections to request handlers
17
25
app . use ( function ( req , res , next ) {
18
- if ( ! collections . articles || ! collections . users ) return next ( new Error ( " No collections." ) )
26
+ if ( ! collections . articles || ! collections . users ) return next ( new Error ( ' No collections.' ) )
19
27
req . collections = collections ;
20
28
return next ( ) ;
21
29
} ) ;
22
30
23
-
24
-
25
- // all environments
31
+ // Express.js configurations
26
32
app . set ( 'port' , process . env . PORT || 3000 ) ;
27
33
app . set ( 'views' , path . join ( __dirname , 'views' ) ) ;
28
34
app . set ( 'view engine' , 'jade' ) ;
29
- app . use ( express . favicon ( ) ) ;
30
- app . use ( express . logger ( 'dev' ) ) ;
31
- app . use ( express . json ( ) ) ;
32
- app . use ( express . cookieParser ( '3CCC4ACD-6ED1-4844-9217-82131BDCB239' ) ) ;
33
- app . use ( express . session ( { secret : '2C44774A-D649-4D44-9535-46E296EF984F' } ) )
34
- app . use ( express . urlencoded ( ) ) ;
35
- app . use ( express . methodOverride ( ) ) ;
35
+
36
+ // Express.js middleware configuration
37
+ app . use ( logger ( 'dev' ) ) ;
38
+ app . use ( bodyParser . json ( ) ) ;
39
+ app . use ( bodyParser . urlencoded ( ) ) ;
40
+ app . use ( cookieParser ( '3CCC4ACD-6ED1-4844-9217-82131BDCB239' ) ) ;
41
+ app . use ( session ( { secret : '2C44774A-D649-4D44-9535-46E296EF984F' } ) )
42
+ app . use ( methodOverride ( ) ) ;
36
43
app . use ( require ( 'stylus' ) . middleware ( __dirname + '/public' ) ) ;
37
44
app . use ( express . static ( path . join ( __dirname , 'public' ) ) ) ;
38
45
46
+ // Authentication middleware
39
47
app . use ( function ( req , res , next ) {
40
48
if ( req . session && req . session . admin )
41
49
res . locals . admin = true ;
42
50
next ( ) ;
43
51
} ) ;
44
52
45
- //authorization
53
+ // Authorization Middleware
46
54
var authorize = function ( req , res , next ) {
47
55
if ( req . session && req . session . admin )
48
56
return next ( ) ;
49
57
else
50
58
return res . send ( 401 ) ;
51
59
} ;
52
60
53
- // development only
54
61
if ( 'development' == app . get ( 'env' ) ) {
55
- app . use ( express . errorHandler ( ) ) ;
62
+ app . use ( errorHandler ( ) ) ;
56
63
}
57
64
58
-
59
- app . use ( app . router ) ;
60
- //PAGES&ROUTES
65
+ // PAGES&ROUTES
61
66
app . get ( '/' , routes . index ) ;
62
67
app . get ( '/login' , routes . user . login ) ;
63
68
app . post ( '/login' , routes . user . authenticate ) ;
@@ -67,7 +72,7 @@ app.get('/post', authorize, routes.article.post);
67
72
app . post ( '/post' , authorize , routes . article . postArticle ) ;
68
73
app . get ( '/articles/:slug' , routes . article . show ) ;
69
74
70
- //REST API ROUTES
75
+ // REST API ROUTES
71
76
app . all ( '/api' , authorize ) ;
72
77
app . get ( '/api/articles' , routes . article . list )
73
78
app . post ( '/api/articles' , routes . article . add ) ;
@@ -76,6 +81,7 @@ app.del('/api/articles/:id', routes.article.del);
76
81
77
82
78
83
84
+
79
85
app . all ( '*' , function ( req , res ) {
80
86
res . send ( 404 ) ;
81
87
} )
0 commit comments