Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added staticDirHandler

  • Loading branch information...
commit e373d1584c1d256429c87920f20e7a815017fef6 1 parent 1bc17e8
@xavi- xavi- authored
Showing with 53 additions and 28 deletions.
  1. +53 −28 index.js
View
81 index.js
@@ -3,46 +3,70 @@
var url = require("url");
var sys = require("sys");
var fs = require("fs");
-
- var staticFileHandler = (function() {
- var buffer = (function() {
- var buffers = {};
+
+ var buffer = (function() {
+ var buffers = {};
+
+ function addBuffer(path) {
+ if(path in buffers) { return; }
- function addBuffer(path) {
- fs.watchFile(path, function() { buffers[path] = null; });
- }
+ buffers[path] = null;
+ fs.watchFile(path, function() { buffers[path] = null; });
+ }
- function getBuffer(path, callback) {
- if(buffers[path]) { callback(null, buffers[path]); }
- else {
- fs.readFile(path, function(err, data) {
- if(err) { callback(err); };
+ function getBuffer(path, callback) {
+ if(buffers[path]) { callback(null, buffers[path]); return; }
+
+ fs.readFile(path, function(err, data) {
+ if(err) { callback(err); };
- buffers[path] = data;
- callback(err, data);
- });
- }
- }
-
- return { add: addBuffer, get: getBuffer };
- })();
+ buffers[path] = data;
+ callback(err, data);
+ });
+ }
- function Handler(path, mime, res) {
+ return { add: addBuffer, get: getBuffer };
+ })();
+
+ var staticFileHandler = (function() {
+ function handler(path, mimeType, res) {
buffer.get(path, function(err, buffer) {
if(err) { throw err; };
res.writeHead(200, { "Conent-Length": buffer.length,
- "Content-Type": mime });
- res.end(buffer);
+ "Content-Type": mimeType });
+ res.end(buffer, "binary");
});
}
-
- return function(path, mime) {
+
+ return function staticFileHandler(path, mime) {
buffer.add(path);
- return function(req, res) { Handler(path, mime, res); };
+ return function(req, res) { handler(path, mime, res); };
};
})();
+ function staticDirHandler(urlPath, fileDir, extension, mimeType) {
+ var regUrl = new RegExp(urlPath + "([/a-zA-Z0-9_-]+)\." + extension);
+
+ return {
+ test: function(req) { return regUrl.test(url.parse(req.url).pathname); },
+ handler: function(req, res) {
+ var uri = url.parse(req.url);
+ var urlName = regUrl.exec(uri.pathname)[1];
+
+ buffer.add(fileDir + urlName + "." + extension);
+
+ buffer.get(fileDir + urlName + "." + extension, function(err, buffer) {
+ if(err) { throw err; }
+
+ res.writeHead(200, { "Content-Length": buffer.length,
+ "Content-Type": mimeType });
+ res.end(buffer, "binary");
+ });
+ }
+ };
+ }
+
var urls = {};
function error(req, res) {
@@ -61,8 +85,8 @@
}
return null;
- }
-
+ }
+
var server = http.createServer(function(req, res) {
(urls[url.parse(req.url).pathname] || findPattern(req) || context.error)(req, res);
});
@@ -72,4 +96,5 @@
context.error = error;
context.server = server;
context.staticFileHandler = staticFileHandler;
+ context.staticDirHandler = staticDirHandler;
})(exports);
Please sign in to comment.
Something went wrong with that request. Please try again.