Send Content-Type headers with static files

Fixes a problem that occurs in Chrome 61 where the following error message appears in the console:

'Failed to load module script: The server responded with a non-JavaScript MIME type of "".
Strict MIME type checking is enforced for module scripts per HTML spec.'
This commit is contained in:
Giannis Kosmas 2017-10-12 19:28:30 +03:00
parent 61879b175c
commit 99198cface
2 changed files with 9 additions and 1 deletions

View File

@ -18,6 +18,7 @@
"dependencies": {
"ws": ">=0.4.27",
"optimist": "latest",
"mime-types" : "latest",
"policyfile": "latest"
}
}

View File

@ -16,6 +16,7 @@ var argv = require('optimist').argv,
url = require('url'),
path = require('path'),
fs = require('fs'),
mime = require('mime-types'),
Buffer = require('buffer').Buffer,
WebSocketServer = require('ws').Server,
@ -107,7 +108,13 @@ http_request = function (request, response) {
return http_error(response, 500, err);
}
response.writeHead(200);
var headers = {};
var contentType = mime.contentType(path.extname(filename));
if (contentType !== false) {
headers['Content-Type'] = contentType;
}
response.writeHead(200, headers);
response.write(file, "binary");
response.end();
});