Permalink
Browse files

Renamed fu to router and made it possible to create multiple servers.

  • Loading branch information...
1 parent 7741f39 commit 1b4c717270e9ad69403b99e666ed53b89b5763f5 @scottgonzalez committed Mar 20, 2010
Showing with 103 additions and 106 deletions.
  1. +98 −0 lib/router/index.js
  2. +5 −106 lib/{fu.js → router/mime.js}
View
@@ -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();
+ });
+ };
+};
@@ -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
- lookupExtension : function(ext, fallback) {
- return fu.mime.TYPES[ext.toLowerCase()] || fallback || 'application/octet-stream';
- },
+ exports.lookupExtension = function(ext, fallback) {
+ return exports.TYPES[ext.toLowerCase()] || fallback || 'application/octet-stream';
+ };
// List of most common mime-types, stolen from Rack.
- TYPES : { ".3gp" : "video/3gpp"
+ exports.TYPES = { ".3gp" : "video/3gpp"
, ".a" : "application/octet-stream"
, ".ai" : "application/postscript"
, ".aif" : "audio/x-aiff"
@@ -269,5 +169,4 @@ fu.mime = {
, ".yaml" : "text/yaml"
, ".yml" : "text/yaml"
, ".zip" : "application/zip"
- }
-};
+ };

0 comments on commit 1b4c717

Please sign in to comment.