@@ -111,7 +111,7 @@ function writeFile(path: string, data: any, opts: { recursive: boolean }) {
111
111
112
112
/// Request Handling ///
113
113
114
- function handleResolutionRequest ( filePath : string , res : http . ServerResponse ) {
114
+ function handleResolutionRequest ( filePath : string , res : http . ServerResponse ) {
115
115
var resolvedPath = path . resolve ( filePath , '' ) ;
116
116
resolvedPath = resolvedPath . substring ( resolvedPath . indexOf ( 'tests' ) ) ;
117
117
resolvedPath = switchToForwardSlashes ( resolvedPath ) ;
@@ -202,14 +202,13 @@ function handleRequestOperation(req: http.ServerRequest, res: http.ServerRespons
202
202
break ;
203
203
case RequestType . GetFile :
204
204
fs . readFile ( reqPath , function ( err , file ) {
205
- var ext = reqPath . substr ( reqPath . lastIndexOf ( '.' ) ) ;
206
- var contentType = 'binary' ;
207
- if ( ext === '.js' ) contentType = 'text/javascript'
208
- else if ( ext === '.css' ) contentType = 'text/javascript'
209
- else if ( ext === '.html' ) contentType = 'text/html'
210
- err
211
- ? send ( 'fail' , res , err . message , contentType )
212
- : send ( 'success' , res , ( < any > file ) , contentType ) ;
205
+ const contentType = contentTypeForExtension ( path . extname ( reqPath ) ) ;
206
+ if ( err ) {
207
+ send ( 'fail' , res , err . message , contentType ) ;
208
+ }
209
+ else {
210
+ send ( 'success' , res , < any > file , contentType ) ;
211
+ }
213
212
} ) ;
214
213
break ;
215
214
case RequestType . ResolveFile :
@@ -249,6 +248,15 @@ function handleRequestOperation(req: http.ServerRequest, res: http.ServerRespons
249
248
send ( 'unknown' , res , null ) ;
250
249
break ;
251
250
}
251
+
252
+ function contentTypeForExtension ( ext : string ) {
253
+ switch ( ext ) {
254
+ case '.js' : return 'text/javascript' ;
255
+ case '.css' : return 'text/css' ;
256
+ case '.html' : return 'text/html' ;
257
+ default : return 'binary' ;
258
+ }
259
+ }
252
260
}
253
261
254
262
console . log ( "Static file server running at\n => http://localhost:" + port + "/\nCTRL + C to shutdown" ) ;
0 commit comments