@@ -22,42 +22,50 @@ const setup = function(fileUploadOptions) {
22
22
app . use ( expressFileupload ( fileUploadOptions ) ) ;
23
23
24
24
app . all ( '/upload/single' , function ( req , res ) {
25
- if ( ! req . files )
25
+ if ( ! req . files ) {
26
26
return res . status ( 400 ) . send ( 'No files were uploaded.' ) ;
27
+ }
27
28
28
29
let testFile = req . files . testFile ;
29
30
let uploadPath = path . join ( uploadDir , testFile . name ) ;
30
31
31
32
testFile . mv ( uploadPath , function ( err ) {
32
- if ( err )
33
+ if ( err ) {
33
34
return res . status ( 500 ) . send ( err ) ;
35
+ }
34
36
35
37
res . send ( 'File uploaded to ' + uploadPath ) ;
36
38
} ) ;
37
39
} ) ;
38
40
39
41
app . all ( '/upload/single/withfields' , function ( req , res ) {
40
- if ( ! req . files )
42
+ if ( ! req . files ) {
41
43
return res . status ( 400 ) . send ( 'No files were uploaded.' ) ;
44
+ }
42
45
43
- if ( ! req . body )
46
+ if ( ! req . body ) {
44
47
return res . status ( 400 ) . send ( 'No request body found' ) ;
48
+ }
45
49
46
- if ( ! req . body . firstName || ! req . body . firstName . trim ( ) )
50
+ if ( ! req . body . firstName || ! req . body . firstName . trim ( ) ) {
47
51
return res . status ( 400 ) . send ( 'Invalid first name' ) ;
52
+ }
48
53
49
- if ( ! req . body . lastName || ! req . body . lastName . trim ( ) )
54
+ if ( ! req . body . lastName || ! req . body . lastName . trim ( ) ) {
50
55
return res . status ( 400 ) . send ( 'Invalid last name' ) ;
56
+ }
51
57
52
- if ( ! req . body . email || ! req . body . email . trim ( ) )
58
+ if ( ! req . body . email || ! req . body . email . trim ( ) ) {
53
59
return res . status ( 400 ) . send ( 'Invalid email' ) ;
60
+ }
54
61
55
62
let testFile = req . files . testFile ;
56
63
let uploadPath = path . join ( uploadDir , testFile . name ) ;
57
64
58
65
testFile . mv ( uploadPath , function ( err ) {
59
- if ( err )
66
+ if ( err ) {
60
67
return res . status ( 500 ) . send ( err ) ;
68
+ }
61
69
62
70
res . json ( {
63
71
firstName : req . body . firstName ,
@@ -68,8 +76,9 @@ const setup = function(fileUploadOptions) {
68
76
} ) ;
69
77
70
78
app . all ( '/upload/multiple' , function ( req , res ) {
71
- if ( ! req . files )
79
+ if ( ! req . files ) {
72
80
return res . status ( 400 ) . send ( 'No files were uploaded.' ) ;
81
+ }
73
82
74
83
let testFile1 = req . files . testFile1 ;
75
84
let testFile2 = req . files . testFile2 ;
@@ -78,26 +87,32 @@ const setup = function(fileUploadOptions) {
78
87
let uploadPath2 = path . join ( uploadDir , testFile2 . name ) ;
79
88
let uploadPath3 = path . join ( uploadDir , testFile3 . name ) ;
80
89
81
- if ( ! testFile1 )
90
+ if ( ! testFile1 ) {
82
91
return res . status ( 400 ) . send ( 'testFile1 was not uploaded' ) ;
92
+ }
83
93
84
- if ( ! testFile2 )
94
+ if ( ! testFile2 ) {
85
95
return res . status ( 400 ) . send ( 'testFile2 was not uploaded' ) ;
96
+ }
86
97
87
- if ( ! testFile3 )
98
+ if ( ! testFile3 ) {
88
99
return res . status ( 400 ) . send ( 'testFile3 was not uploaded' ) ;
100
+ }
89
101
90
102
testFile1 . mv ( uploadPath1 , function ( err ) {
91
- if ( err )
103
+ if ( err ) {
92
104
return res . status ( 500 ) . send ( err ) ;
105
+ }
93
106
94
107
testFile2 . mv ( uploadPath2 , function ( err ) {
95
- if ( err )
108
+ if ( err ) {
96
109
return res . status ( 500 ) . send ( err ) ;
110
+ }
97
111
98
112
testFile3 . mv ( uploadPath3 , function ( err ) {
99
- if ( err )
113
+ if ( err ) {
100
114
return res . status ( 500 ) . send ( err ) ;
115
+ }
101
116
102
117
res . send ( 'Files uploaded to ' + uploadDir ) ;
103
118
} ) ;
@@ -106,46 +121,56 @@ const setup = function(fileUploadOptions) {
106
121
} ) ;
107
122
108
123
app . all ( '/upload/array' , function ( req , res ) {
109
- if ( ! req . files )
124
+ if ( ! req . files ) {
110
125
return res . status ( 400 ) . send ( 'No files were uploaded.' ) ;
126
+ }
111
127
112
128
let testFiles = req . files . testFiles ;
113
129
114
- if ( ! testFiles )
130
+ if ( ! testFiles ) {
115
131
return res . status ( 400 ) . send ( 'No files were uploaded' ) ;
132
+ }
116
133
117
- if ( ! Array . isArray ( testFiles ) )
134
+ if ( ! Array . isArray ( testFiles ) ) {
118
135
return res . status ( 400 ) . send ( 'Files were not uploaded as an array' ) ;
136
+ }
119
137
120
- if ( ! testFiles . length )
138
+ if ( ! testFiles . length ) {
121
139
return res . status ( 400 ) . send ( 'Files array is empty' ) ;
140
+ }
122
141
123
142
let filesUploaded = 0 ;
124
143
for ( let i = 0 ; i < testFiles . length ; i ++ ) {
125
144
let uploadPath = path . join ( uploadDir , testFiles [ i ] . name ) ;
126
145
127
146
testFiles [ i ] . mv ( uploadPath , function ( err ) {
128
- if ( err )
147
+ if ( err ) {
129
148
return res . status ( 500 ) . send ( err ) ;
149
+ }
130
150
131
- if ( ++ filesUploaded === testFiles . length )
151
+ if ( ++ filesUploaded === testFiles . length ) {
132
152
res . send ( 'File uploaded to ' + uploadPath ) ;
153
+ }
133
154
} ) ;
134
155
}
135
156
} ) ;
136
157
137
158
app . all ( '/fields/user' , function ( req , res ) {
138
- if ( ! req . body )
159
+ if ( ! req . body ) {
139
160
return res . status ( 400 ) . send ( 'No request body found' ) ;
161
+ }
140
162
141
- if ( ! req . body . firstName || ! req . body . firstName . trim ( ) )
163
+ if ( ! req . body . firstName || ! req . body . firstName . trim ( ) ) {
142
164
return res . status ( 400 ) . send ( 'Invalid first name' ) ;
165
+ }
143
166
144
- if ( ! req . body . lastName || ! req . body . lastName . trim ( ) )
167
+ if ( ! req . body . lastName || ! req . body . lastName . trim ( ) ) {
145
168
return res . status ( 400 ) . send ( 'Invalid last name' ) ;
169
+ }
146
170
147
- if ( ! req . body . email || ! req . body . email . trim ( ) )
171
+ if ( ! req . body . email || ! req . body . email . trim ( ) ) {
148
172
return res . status ( 400 ) . send ( 'Invalid email' ) ;
173
+ }
149
174
150
175
res . json ( {
151
176
firstName : req . body . firstName ,
@@ -155,14 +180,17 @@ const setup = function(fileUploadOptions) {
155
180
} ) ;
156
181
157
182
app . all ( '/fields/array' , function ( req , res ) {
158
- if ( ! req . body )
183
+ if ( ! req . body ) {
159
184
return res . status ( 400 ) . send ( 'No request body found' ) ;
185
+ }
160
186
161
- if ( ! req . body . testField )
187
+ if ( ! req . body . testField ) {
162
188
return res . status ( 400 ) . send ( 'Invalid field' ) ;
189
+ }
163
190
164
- if ( ! Array . isArray ( req . body . testField ) )
191
+ if ( ! Array . isArray ( req . body . testField ) ) {
165
192
return res . status ( 400 ) . send ( 'Field is not an array' ) ;
193
+ }
166
194
167
195
res . json ( req . body . testField ) ;
168
196
} ) ;
0 commit comments