@@ -3,6 +3,7 @@ import * as http from 'http';
33import * as path from "path" ;
44import { ITemplate } from 'inflate-template' ;
55import { ILog } from "./index" ;
6+ import * as mime from "mime-types" ;
67
78/**
89 * Defines an in-memory file served by the web server.
@@ -96,20 +97,6 @@ export class WebServer implements IWebServer {
9697 return "http://127.0.0.1:" + this . assignedPortNo ;
9798 }
9899
99- //
100- // Load a file from the template, caching it as necesary.
101- //
102- private async loadTemplateFile ( template : ITemplate , url : string ) : Promise < Buffer > {
103- const fileSystemPath = path . join ( ...url . split ( '/' ) ) ;
104- const templateFile = template . find ( fileSystemPath ) ;
105- if ( ! templateFile ) {
106- throw new Error ( "Couldn't find file '" + url + "' in template." ) ;
107- }
108-
109- const expandedFileContent = await templateFile . expand ( ) ;
110- return expandedFileContent ;
111- }
112-
113100 /**
114101 * Start the web-server.
115102 */
@@ -121,7 +108,19 @@ export class WebServer implements IWebServer {
121108 app . use ( "/" , async ( request , response , next ) => {
122109 try {
123110 const fileName = request . url === "/" ? "index.html" : request . url ;
124- response . send ( await this . loadTemplateFile ( template , fileName ) ) ;
111+ const fileSystemPath = path . join ( ...fileName . split ( '/' ) ) ;
112+ const templateFile = template . find ( fileSystemPath ) ;
113+ if ( ! templateFile ) {
114+ throw new Error ( "Couldn't find file '" + fileSystemPath + "' in template." ) ;
115+ }
116+
117+ const fileContent = await templateFile . expand ( ) ;
118+ const fileExt = path . extname ( fileName ) ;
119+ const mimeType = mime . lookup ( fileExt ) ;
120+ if ( mimeType ) {
121+ response . setHeader ( "content-type" , mimeType ) ;
122+ }
123+ response . send ( fileContent ) ;
125124 }
126125 catch ( err ) {
127126 this . error ( "Error loading template file." ) ;
0 commit comments