From 587cfd6447ce9d9b1b9bd77d480c7538cfb9cb97 Mon Sep 17 00:00:00 2001 From: Mihai Sucan Date: Tue, 15 Feb 2011 19:50:15 +0800 Subject: [PATCH] add content type detection to static.js. no APIs for file mimetypes yet in nodejs ... --- static.js | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) mode change 100644 => 100755 static.js diff --git a/static.js b/static.js old mode 100644 new mode 100755 index a08f00bf..05af810a --- a/static.js +++ b/static.js @@ -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"); \ No newline at end of file +}).listen(port, "0.0.0.0");