@@ -9,9 +9,35 @@ const indexRouter = require('./routes/index');
9
9
const uploadRouter = require ( './routes/upload' ) ;
10
10
const downloadRouter = require ( './routes/download' ) ;
11
11
12
- // Sets the format to be used for logging
13
- const format =
14
- ':method request for :url resulted in status code :status - :response-time ms' ;
12
+ /*
13
+ * Sets up logging for the app
14
+ *
15
+ * Takes express app and adds logger to it
16
+ */
17
+ function setLogging ( app , loggingLevel , stdout ) {
18
+ // Sets the format to be used for logging
19
+ const format =
20
+ ':method request for :url resulted in status code :status - :response-time ms' ;
21
+
22
+ if ( isNaN ( loggingLevel ) || loggingLevel > 2 || loggingLevel < 0 )
23
+ loggingLevel = 0 ;
24
+
25
+ stdout = stdout || process . stdout ;
26
+
27
+ if ( loggingLevel === 2 )
28
+ app . use (
29
+ logger ( format , {
30
+ stream : stdout ,
31
+ } )
32
+ ) ;
33
+ else if ( loggingLevel === 1 )
34
+ app . use (
35
+ logger ( format , {
36
+ stream : stdout ,
37
+ skip : ( req , res ) => res . statusCode < 400 ,
38
+ } )
39
+ ) ;
40
+ }
15
41
16
42
/*
17
43
* Initialises an Express app with the view generator, plugins and routes
@@ -58,28 +84,6 @@ function handlers(app) {
58
84
} ) ;
59
85
}
60
86
61
- /*
62
- * Sets valid values for keys of the options which may be invaild
63
- *
64
- * Takes options object which is modified and returns nothing
65
- */
66
- function cleanOptions ( options ) {
67
- if (
68
- isNaN ( options . loggingLevel ) ||
69
- options . loggingLevel > 2 ||
70
- options . loggingLevel < 0
71
- )
72
- options . loggingLevel = 0 ;
73
- if ( options . restart === undefined ) options . restart = true ;
74
- if ( ! Array . isArray ( options . files ) ) options . files = [ ] ;
75
- if ( ! options . destination )
76
- options . destination = path . resolve (
77
- path . basename ( require . main . filename ) ,
78
- '../uploads'
79
- ) ;
80
- if ( ! options . stdout ) options . stdout = process . stdout ;
81
- }
82
-
83
87
/*
84
88
* Creates an Express app with the options specified
85
89
*
@@ -88,23 +92,14 @@ function cleanOptions(options) {
88
92
module . exports . init = ( options = { } ) => {
89
93
const app = express ( ) ;
90
94
91
- cleanOptions ( options ) ;
92
-
93
- if ( options . loggingLevel === 2 )
94
- app . use (
95
- logger ( format , {
96
- stream : options . stdout ,
97
- } )
98
- ) ;
99
- else if ( options . loggingLevel === 1 )
100
- app . use (
101
- logger ( format , {
102
- stream : options . stdout ,
103
- skip : ( req , res ) => res . statusCode < 400 ,
104
- } )
105
- ) ;
95
+ // Set logging only if neccessary
96
+ if ( options . loggingLevel )
97
+ setLogging ( app , options . loggingLevel , options . stdout ) ;
106
98
107
99
return new Promise ( ( resolve , reject ) => {
100
+ // If restart flag is not set, default to true
101
+ if ( options . restart === undefined ) options . restart = true ;
102
+
108
103
require ( './middleware/fileManager' )
109
104
. initFiles ( options )
110
105
. then ( ( ) => {
0 commit comments