Merge pull request #432 from h3raq/patch-1

Adding Content-Type support!
This commit is contained in:
hamza zia 2018-02-10 18:21:37 +08:00 committed by GitHub
commit 63e1469f68
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -8,6 +8,16 @@ http.createServer(function(request, response) {
var uri = url.parse(request.url).pathname, var uri = url.parse(request.url).pathname,
filename = path.join(process.cwd(), uri); filename = path.join(process.cwd(), uri);
var extname = path.extname(filename);
var contentType = 'text/html';
switch (extname) {
case '.js': contentType = 'text/javascript'; break;
case '.css': contentType = 'text/css'; break;
case '.ico': contentType = 'image/x-icon'; break;
case '.svg': contentType = 'image/svg+xml'; break;
}
fs.exists(filename, function(exists) { fs.exists(filename, function(exists) {
if(!exists) { if(!exists) {
response.writeHead(404, {"Content-Type": "text/plain"}); response.writeHead(404, {"Content-Type": "text/plain"});
@ -25,7 +35,7 @@ http.createServer(function(request, response) {
response.end(); response.end();
return; return;
} }
response.writeHead(200); response.writeHead(200, {'Content-Type': contentType});
response.write(file, "binary"); response.write(file, "binary");
response.end(); response.end();
}); });