Slide 22
Slide 22 text
static.js
".flv" : "video/x-flv",
".for" : "text/x-fortran",
".gem" : "application/octet-stream",
".gemspec" : "text/x-script.ruby",
".gif" : "image/gif",
".gz" : "application/x-gzip",
".h" : "text/x-c",
".hh" : "text/x-c",
".htm" : "text/html",
".html" : "text/html",
".ico" : "image/vnd.microsoft.icon",
".ics" : "text/calendar",
".ifb" : "text/calendar",
".iso" : "application/octet-stream",
".jar" : "application/java-archive",
".java" : "text/x-java-source",
".jnlp" : "application/x-java-jnlp-file",
".jpeg" : "image/jpeg",
".jpg" : "image/jpeg",
".js" : "application/javascript",
".json" : "application/json",
".log" : "text/plain",
".m3u" : "audio/x-mpegurl",
".m4v" : "video/mp4",
".man" : "text/troff",
".mathml" : "application/mathml+xml",
".mbox" : "application/mbox",
".mdoc" : "text/troff",
".me" : "text/troff",
".mid" : "audio/midi",
// Buffer any events that fire while waiting on the stat.
var events = [];
function onData() {
events.push(["data"].concat(Array.prototype.slice.call(arguments)));
}
function onEnd() {
events.push(["end"].concat(Array.prototype.slice.call(arguments)));
}
req.addListener("data", onData);
req.addListener("end", onEnd);
fs.stat(filename, function (err, stat) {
// Stop buffering events
req.removeListener("data", onData);
req.removeListener("end", onEnd);
// Fall through for missing files, thow error for other problems
if (err) {
if (err.errno === process.ENOENT) {
next();
// Refire the buffered events
events.forEach(function (args) {
req.emit.apply(req, args);
});
return;
(err);
rn;
the file directly using buffers
ile(filename, function (err, data) {
err) {
next(err);
return;
writeHead(200, {
ontent-Type": Mime.type(filename),
ontent-Length": data.length,
ast-Modified": stat.mtime.toUTCString(),
/ Cache in browser for 1 year
ache-Control": "public max-age=" + 31536000
end(data);
".bmp" : "image/bmp",
".bz2" : "application/x-bzip2",
".c" : "text/x-c",
".cab" : "application/vnd.ms-cab-compressed",
".cc" : "text/x-c",
".chm" : "application/vnd.ms-htmlhelp",
".class" : "application/octet-stream",
".com" : "application/x-msdownload",
".conf" : "text/plain",
".cpp" : "text/x-c",
".crt" : "application/x-x509-ca-cert",
".css" : "text/css",
".csv" : "text/csv",
".cxx" : "text/x-c",
".deb" : "application/x-debian-package",
".der" : "application/x-x509-ca-cert",
".diff" : "text/x-diff",
".djv" : "image/vnd.djvu",
".djvu" : "image/vnd.djvu",
".dll" : "application/x-msdownload",
".dmg" : "application/octet-stream",
".doc" : "application/msword",
".dot" : "application/msword",
".dtd" : "application/xml-dtd",
".dvi" : "application/x-dvi",
".ear" : "application/java-archive",
".eml" : "message/rfc822",
".eps" : "application/postscript",
".exe" : "application/x-msdownload",
".f" : "text/x-fortran",
".f77" : "text/x-fortran",
".f90" : "text/x-fortran",
};
// Mini mime module for static file serving
var Mime = {
type: function getMime(path) {
var index = path.lastIndexOf(".");
if (index < 0) {
return DEFAULT_MIME;
}
var type = Mime.TYPES[path.substring(index).toLowerCase()] || DEFAULT_MIME;
return (/(text|javascript)/).test(type) ? type + "; charset=utf-8" : type;
},
TYPES : { ".3gp" : "video/3gpp",
".a" : "application/octet-stream",
".ai" : "application/postscript",
".aif" : "audio/x-aiff",
".aiff" : "audio/x-aiff",
".asc" : "application/pgp-signature",
".asf" : "video/x-ms-asf",
".asm" : "text/x-asm",
".asx" : "video/x-ms-asf",
".atom" : "application/atom+xml",
".au" : "audio/basic",
".avi" : "video/x-msvideo",
var fs = require('fs'),
Url = require('url'),
Path = require('path');
var lifetime = 1000 * 60 * 60; // 1 hour browser cache lifetime
var DEFAULT_MIME = 'application/octet-stream';
module.exports = {
setup: function (env) {
this.root = this.root || process.cwd();
},
handle: function (err, req, res, next) {
// Skip on error
if (err) {
next();
return;
}
var url = Url.parse(req.url);
var pathname = url.pathname.replace(/\.\.+/g, '.'),
filename = Path.join(this.root, pathname);
if (filename[filename.length - 1] === "/") {
filename += "index.html";
}
Saturday, June 5, 2010