add content type detection to static.js. no APIs for file mimetypes yet in nodejs ...
This commit is contained in:
parent
695a2420a6
commit
587cfd6447
1 changed files with 19 additions and 2 deletions
19
static.js
Normal file → Executable file
19
static.js
Normal file → Executable file
|
|
@ -1,9 +1,26 @@
|
||||||
|
#!/usr/bin/env node
|
||||||
|
|
||||||
var http = require("http"),
|
var http = require("http"),
|
||||||
url = require("url"),
|
url = require("url"),
|
||||||
path = require("path"),
|
path = require("path"),
|
||||||
fs = require("fs")
|
fs = require("fs")
|
||||||
port = process.env.C9_PORT || 8888;
|
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) {
|
http.createServer(function(request, response) {
|
||||||
|
|
||||||
var uri = url.parse(request.url).pathname
|
var uri = url.parse(request.url).pathname
|
||||||
|
|
@ -27,7 +44,7 @@ http.createServer(function(request, response) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
response.writeHead(200);
|
response.writeHead(200, {"Content-Type": guessFileType(filename)});
|
||||||
response.write(file, "binary");
|
response.write(file, "binary");
|
||||||
response.end();
|
response.end();
|
||||||
});
|
});
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue