Skip to content

Commit

Permalink
Renamed fu to router and made it possible to create multiple servers.
Browse files Browse the repository at this point in the history
  • Loading branch information
scottgonzalez committed Mar 20, 2010
1 parent 7741f39 commit 1b4c717
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 106 deletions.
98 changes: 98 additions & 0 deletions lib/router/index.js
@@ -0,0 +1,98 @@
var createServer = require("http").createServer,
readFile = require("fs").readFile,
sys = require("sys"),
url = require("url"),
mime = require("./mime");

var router = exports;
var NOT_FOUND = "Not Found\n";

function notFound(req, res) {
res.sendHeader(404, [
["Content-Type", "text/plain"],
["Content-Length", NOT_FOUND.length]
]);
res.write(NOT_FOUND);
res.close();
}

router.createServer = function() {
var getMap = {};

server = createServer(function(req, res) {
if (req.method === "GET" || req.method === "HEAD") {
var handler = getMap[url.parse(req.url).pathname] || notFound;

res.simpleText = function (code, body) {
res.sendHeader(code, [ ["Content-Type", "text/plain"]
, ["Content-Length", body.length]
]);
res.write(body);
res.close();
};

res.simpleJSON = function (code, obj) {
var body = JSON.stringify(obj);
res.sendHeader(code, [ ["Content-Type", "text/json"]
, ["Content-Length", body.length]
]);
res.write(body);
res.close();
};

handler(req, res);
}
});

return {
listen: function(port, host) {
server.listen(port, host);
sys.puts("Server at http://" + (host || "127.0.0.1") + ":" + port.toString() + "/");
},
get: function (path, handler) {
getMap[path] = handler;
}
};
};

function extname (path) {
var index = path.lastIndexOf(".");
return index < 0 ? "" : path.substring(index);
}

router.staticHandler = function (filename) {
var body, headers;
var content_type = mime.lookupExtension(extname(filename));
var encoding = (content_type.slice(0,4) === "text" ? "utf8" : "binary");

function loadResponseData(callback) {
if (body && headers) {
callback();
return;
}

sys.puts("loading " + filename + "...");
readFile(filename, encoding, function (err, data) {
if (err) {
sys.puts("Error loading " + filename);
} else {
body = data;
headers = [ [ "Content-Type" , content_type ]
, [ "Content-Length" , body.length ]
];
headers.push(["Cache-Control", "public"]);

sys.puts("static file " + filename + " loaded");
callback();
}
});
}

return function (req, res) {
loadResponseData(function () {
res.sendHeader(200, headers);
res.write(body, encoding);
res.close();
});
};
};
111 changes: 5 additions & 106 deletions lib/fu.js → lib/router/mime.js
@@ -1,110 +1,10 @@
var createServer = require("http").createServer;
var readFile = require("fs").readFile;
var sys = require("sys");
var url = require("url");
DEBUG = false;

var fu = exports;

var NOT_FOUND = "Not Found\n";

function notFound(req, res) {
res.sendHeader(404, [ ["Content-Type", "text/plain"]
, ["Content-Length", NOT_FOUND.length]
]);
res.write(NOT_FOUND);
res.close();
}

var getMap = {};

fu.get = function (path, handler) {
getMap[path] = handler;
};
var server = createServer(function (req, res) {
if (req.method === "GET" || req.method === "HEAD") {
var handler = getMap[url.parse(req.url).pathname] || notFound;

res.simpleText = function (code, body) {
res.sendHeader(code, [ ["Content-Type", "text/plain"]
, ["Content-Length", body.length]
]);
res.write(body);
res.close();
};

res.simpleJSON = function (code, obj) {
var body = JSON.stringify(obj);
res.sendHeader(code, [ ["Content-Type", "text/json"]
, ["Content-Length", body.length]
]);
res.write(body);
res.close();
};

handler(req, res);
}
});

fu.listen = function (port, host) {
server.listen(port, host);
sys.puts("Server at http://" + (host || "127.0.0.1") + ":" + port.toString() + "/");
};

fu.close = function () { server.close(); };

function extname (path) {
var index = path.lastIndexOf(".");
return index < 0 ? "" : path.substring(index);
}

fu.staticHandler = function (filename) {
var body, headers;
var content_type = fu.mime.lookupExtension(extname(filename));
var encoding = (content_type.slice(0,4) === "text" ? "utf8" : "binary");

function loadResponseData(callback) {
if (body && headers && !DEBUG) {
callback();
return;
}

sys.puts("loading " + filename + "...");
readFile(filename, encoding, function (err, data) {
if (err) {
sys.puts("Error loading " + filename);
} else {
body = data;
headers = [ [ "Content-Type" , content_type ]
, [ "Content-Length" , body.length ]
];
if (!DEBUG)
headers.push(["Cache-Control", "public"]);

sys.puts("static file " + filename + " loaded");
callback();
}
});
}

return function (req, res) {
loadResponseData(function () {
res.sendHeader(200, headers);
res.write(body, encoding);
res.close();
});
}
};

// stolen from jack- thanks
fu.mime = {
// returns MIME type for extension, or fallback, or octet-steam // returns MIME type for extension, or fallback, or octet-steam
lookupExtension : function(ext, fallback) { exports.lookupExtension = function(ext, fallback) {
return fu.mime.TYPES[ext.toLowerCase()] || fallback || 'application/octet-stream'; return exports.TYPES[ext.toLowerCase()] || fallback || 'application/octet-stream';
}, };


// List of most common mime-types, stolen from Rack. // List of most common mime-types, stolen from Rack.
TYPES : { ".3gp" : "video/3gpp" exports.TYPES = { ".3gp" : "video/3gpp"
, ".a" : "application/octet-stream" , ".a" : "application/octet-stream"
, ".ai" : "application/postscript" , ".ai" : "application/postscript"
, ".aif" : "audio/x-aiff" , ".aif" : "audio/x-aiff"
Expand Down Expand Up @@ -269,5 +169,4 @@ fu.mime = {
, ".yaml" : "text/yaml" , ".yaml" : "text/yaml"
, ".yml" : "text/yaml" , ".yml" : "text/yaml"
, ".zip" : "application/zip" , ".zip" : "application/zip"
} };
};

0 comments on commit 1b4c717

Please sign in to comment.