add content type detection to static.js. no APIs for file mimetypes yet in nodejs ...

This commit is contained in:
Mihai Sucan 2011-02-15 19:50:15 +08:00 committed by Fabian Jakobs
commit 587cfd6447

21
static.js Normal file → Executable file
View file

@ -1,9 +1,26 @@
#!/usr/bin/env node
var http = require("http"),
url = require("url"),
path = require("path"),
fs = require("fs")
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
@ -27,9 +44,9 @@ http.createServer(function(request, response) {
return;
}
response.writeHead(200);
response.writeHead(200, {"Content-Type": guessFileType(filename)});
response.write(file, "binary");
response.end();
});
});
}).listen(port, "0.0.0.0");
}).listen(port, "0.0.0.0");