11'use strict' ;
22
33var 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' ) ,
411 path = require ( 'path' ) ,
512 config = require ( './config' ) < % if ( mongoPassportUser ) { % > ,
613 passport = require ( 'passport' ) ,
7- mongoStore = require ( 'connect-mongo' ) ( express ) < % } % > ;
14+ mongoStore = require ( 'connect-mongo' ) ( session ) < % } % > ;
815
916/**
1017 * Express configuration
1118 */
1219module . exports = function ( app ) {
13- app . configure ( 'development' , function ( ) {
14- app . use ( require ( 'connect-livereload' ) ( ) ) ;
20+ var env = app . get ( 'env' ) ;
1521
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' ) ( ) ) ;
2524
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+ } ) ;
3034
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+ }
3639
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 ) { % >
3847 app . engine ( 'html' , require ( 'ejs' ) . renderFile ) ;
3948 app . set ( 'view engine' , 'html' ) ; < % } % > < % if ( jade ) { % >
4049 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 ( ) ) ;
4654
4755 // 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+ } )
5664 } ) ) ;
5765
58- //use passport session
66+ // Use passport session
5967 app . use ( passport . initialize ( ) ) ;
6068 app . use ( passport . session ( ) ) ;
6169 < % } % >
62- // Router (only error handlers should come after this)
63- app . use ( app . router ) ;
64- } ) ;
6570
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+ }
7075} ;
0 commit comments