@@ -208,11 +208,7 @@ serveIndex.html = function _html(req, res, files, next, dir, showUp, icons, path
208
208
// render html
209
209
render ( locals , function ( err , body ) {
210
210
if ( err ) return next ( err ) ;
211
-
212
- var buf = new Buffer ( body , 'utf8' ) ;
213
- res . setHeader ( 'Content-Type' , 'text/html; charset=utf-8' ) ;
214
- res . setHeader ( 'Content-Length' , buf . length ) ;
215
- res . end ( buf ) ;
211
+ send ( res , 'text/html' , body )
216
212
} ) ;
217
213
} ) ;
218
214
} ) ;
@@ -223,25 +219,15 @@ serveIndex.html = function _html(req, res, files, next, dir, showUp, icons, path
223
219
*/
224
220
225
221
serveIndex . json = function _json ( req , res , files ) {
226
- var body = JSON . stringify ( files ) ;
227
- var buf = new Buffer ( body , 'utf8' ) ;
228
-
229
- res . setHeader ( 'Content-Type' , 'application/json; charset=utf-8' ) ;
230
- res . setHeader ( 'Content-Length' , buf . length ) ;
231
- res . end ( buf ) ;
222
+ send ( res , 'application/json' , JSON . stringify ( files ) )
232
223
} ;
233
224
234
225
/**
235
226
* Respond with text/plain.
236
227
*/
237
228
238
229
serveIndex . plain = function _plain ( req , res , files ) {
239
- var body = files . join ( '\n' ) + '\n' ;
240
- var buf = new Buffer ( body , 'utf8' ) ;
241
-
242
- res . setHeader ( 'Content-Type' , 'text/plain; charset=utf-8' ) ;
243
- res . setHeader ( 'Content-Length' , buf . length ) ;
244
- res . end ( buf ) ;
230
+ send ( res , 'text/plain' , ( files . join ( '\n' ) + '\n' ) )
245
231
} ;
246
232
247
233
/**
@@ -503,6 +489,17 @@ function removeHidden(files) {
503
489
} ) ;
504
490
}
505
491
492
+ /**
493
+ * Send a response.
494
+ * @private
495
+ */
496
+
497
+ function send ( res , type , body ) {
498
+ res . setHeader ( 'Content-Type' , type + '; charset=utf-8' )
499
+ res . setHeader ( 'Content-Length' , Buffer . byteLength ( body , 'utf8' ) )
500
+ res . end ( body , 'utf8' )
501
+ }
502
+
506
503
/**
507
504
* Stat all files and return array of stat
508
505
* in same order.
0 commit comments