1
1
'use strict' ;
2
2
3
3
var express = require ( 'express' ) ,
4
+ favicon = require ( 'static-favicon' ) ,
5
+ morgan = require ( 'morgan' ) ,
6
+ bodyParser = require ( 'body-parser' ) ,
7
+ methodOverride = require ( 'method-override' ) ,
8
+ cookieParser = require ( 'cookie-parser' ) ,
9
+ session = require ( 'express-session' ) ,
10
+ errorHandler = require ( 'errorhandler' ) ,
4
11
path = require ( 'path' ) ,
5
12
config = require ( './config' ) < % if ( mongoPassportUser ) { % > ,
6
13
passport = require ( 'passport' ) ,
7
- mongoStore = require ( 'connect-mongo' ) ( express ) < % } % > ;
14
+ mongoStore = require ( 'connect-mongo' ) ( session ) < % } % > ;
8
15
9
16
/**
10
17
* Express configuration
11
18
*/
12
19
module . exports = function ( app ) {
13
- app . configure ( 'development' , function ( ) {
14
- app . use ( require ( 'connect-livereload' ) ( ) ) ;
20
+ var env = app . get ( 'env' ) ;
15
21
16
- // Disable caching of scripts for easier testing
17
- app . use ( function noCache ( req , res , next ) {
18
- if ( req . url . indexOf ( '/scripts/' ) === 0 ) {
19
- res . header ( 'Cache-Control' , 'no-cache, no-store, must-revalidate' ) ;
20
- res . header ( 'Pragma' , 'no-cache' ) ;
21
- res . header ( 'Expires' , 0 ) ;
22
- }
23
- next ( ) ;
24
- } ) ;
22
+ if ( 'development' === env ) {
23
+ app . use ( require ( 'connect-livereload' ) ( ) ) ;
25
24
26
- app . use ( express . static ( path . join ( config . root , '.tmp' ) ) ) ;
27
- app . use ( express . static ( path . join ( config . root , 'app' ) ) ) ;
28
- app . set ( 'views' , config . root + '/app/views' ) ;
29
- } ) ;
25
+ // Disable caching of scripts for easier testing
26
+ app . use ( function noCache ( req , res , next ) {
27
+ if ( req . url . indexOf ( '/scripts/' ) === 0 ) {
28
+ res . header ( 'Cache-Control' , 'no-cache, no-store, must-revalidate' ) ;
29
+ res . header ( 'Pragma' , 'no-cache' ) ;
30
+ res . header ( 'Expires' , 0 ) ;
31
+ }
32
+ next ( ) ;
33
+ } ) ;
30
34
31
- app . configure ( 'production' , function ( ) {
32
- app . use ( express . favicon ( path . join ( config . root , 'public' , 'favicon.ico' ) ) ) ;
33
- app . use ( express . static ( path . join ( config . root , 'public' ) ) ) ;
34
- app . set ( 'views' , config . root + '/views' ) ;
35
- } ) ;
35
+ app . use ( express . static ( path . join ( config . root , '.tmp' ) ) ) ;
36
+ app . use ( express . static ( path . join ( config . root , 'app' ) ) ) ;
37
+ app . set ( 'views' , config . root + '/app' ) ;
38
+ }
36
39
37
- app . configure ( function ( ) { < % if ( ! jade ) { % >
40
+ if ( 'production' === env ) {
41
+ app . use ( favicon ( path . join ( config . root , 'public' , 'favicon.ico' ) ) ) ;
42
+ app . use ( express . static ( path . join ( config . root , 'public' ) ) ) ;
43
+ app . set ( 'views' , config . root + '/views' ) ;
44
+ }
45
+
46
+ < % if ( ! jade ) { % >
38
47
app . engine ( 'html' , require ( 'ejs' ) . renderFile ) ;
39
48
app . set ( 'view engine' , 'html' ) ; < % } % > < % if ( jade ) { % >
40
49
app . set ( 'view engine' , 'jade' ) ; < % } % >
41
- app . use ( express . logger ( 'dev' ) ) ;
42
- app . use ( express . json ( ) ) ;
43
- app . use ( express . urlencoded ( ) ) ;
44
- app . use ( express . methodOverride ( ) ) ; < % if ( mongoPassportUser ) { % >
45
- app . use ( express . cookieParser ( ) ) ;
50
+ app . use ( morgan ( 'dev' ) ) ;
51
+ app . use ( bodyParser ( ) ) ;
52
+ app . use ( methodOverride ( ) ) ; < % if ( mongoPassportUser ) { % >
53
+ app . use ( cookieParser ( ) ) ;
46
54
47
55
// Persist sessions with mongoStore
48
- app . use ( express . session ( {
49
- secret : 'angular-fullstack secret' ,
50
- store : new mongoStore ( {
51
- url : config . mongo . uri ,
52
- collection : 'sessions'
53
- } , function ( ) {
54
- console . log ( " db connection open" ) ;
55
- } )
56
+ app . use ( session ( {
57
+ secret : 'angular-fullstack secret' ,
58
+ store : new mongoStore ( {
59
+ url : config . mongo . uri ,
60
+ collection : 'sessions'
61
+ } , function ( ) {
62
+ console . log ( ' db connection open' ) ;
63
+ } )
56
64
} ) ) ;
57
65
58
- //use passport session
66
+ // Use passport session
59
67
app . use ( passport . initialize ( ) ) ;
60
68
app . use ( passport . session ( ) ) ;
61
69
< % } % >
62
- // Router (only error handlers should come after this)
63
- app . use ( app . router ) ;
64
- } ) ;
65
70
66
- // Error handler
67
- app . configure ( 'development' , function ( ) {
68
- app . use ( express . errorHandler ( ) ) ;
69
- } ) ;
71
+ // Error handler - has to be last
72
+ if ( 'development' === app . get ( 'env' ) ) {
73
+ app . use ( errorHandler ( ) )
74
+ }
70
75
} ;
0 commit comments