a better approach: use the node-mime plugin

This commit is contained in:
Mihai Sucan 2011-02-15 20:09:19 +08:00 • committed by Fabian Jakobs
commit 32b674ce7f
2 changed files with 7 additions and 19 deletions

View file

@ -3,24 +3,10 @@
var http = require("http"),
url = require("url"),
path = require("path"),
fs = require("fs")
fs = require("fs"),
mime = require("mime"),
port = process.env.C9_PORT || 8888;
function guessFileType(name) {
var types = {
'.html': 'text/html',
'.xhtml': 'application/xhtml+xml',
'.js': 'text/javascript',
'.css': 'text/css',
'.png': 'image/png',
'.jpg': 'image/jpeg',
};
var ext = path.extname(name);
return ext in types ? types[ext] : 'text/plain';
}
http.createServer(function(request, response) {
var uri = url.parse(request.url).pathname
@ -44,7 +30,8 @@ http.createServer(function(request, response) {
return;
}
response.writeHead(200, {"Content-Type": guessFileType(filename)});
var contentType = mime.lookup(filename) || "text/plain";
response.writeHead(200, {"Content-Type": contentType});
response.write(file, "binary");
response.end();
});