@@ -111,7 +111,7 @@ function writeFile(path: string, data: any, opts: { recursive: boolean }) {
111111
112112/// Request Handling ///
113113
114- function handleResolutionRequest ( filePath : string , res : http . ServerResponse ) {
114+ function handleResolutionRequest ( filePath : string , res : http . ServerResponse ) {
115115 var resolvedPath = path . resolve ( filePath , '' ) ;
116116 resolvedPath = resolvedPath . substring ( resolvedPath . indexOf ( 'tests' ) ) ;
117117 resolvedPath = switchToForwardSlashes ( resolvedPath ) ;
@@ -202,14 +202,13 @@ function handleRequestOperation(req: http.ServerRequest, res: http.ServerRespons
202202 break ;
203203 case RequestType . GetFile :
204204 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+ }
213212 } ) ;
214213 break ;
215214 case RequestType . ResolveFile :
@@ -249,6 +248,15 @@ function handleRequestOperation(req: http.ServerRequest, res: http.ServerRespons
249248 send ( 'unknown' , res , null ) ;
250249 break ;
251250 }
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+ }
252260}
253261
254262console . log ( "Static file server running at\n => http://localhost:" + port + "/\nCTRL + C to shutdown" ) ;
0 commit comments