Skip to content
Browse files

Fixed to support the new style of url parsing. So the project will no…

…w work with the most recent version of node
  • Loading branch information...
1 parent 097e020 commit aa9442ef595059f0e3e0cb7d927253837805a68f Robert Righter committed
Showing with 29 additions and 28 deletions.
  1. +23 −25 lib/fu.js
  2. +6 −3 pollDataServer.js
View
48 lib/fu.js
@@ -1,13 +1,13 @@
var createServer = require("http").createServer;
+var process = require("posix");
var sys = require("sys");
-var posix = require("posix");
-
+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]
@@ -15,17 +15,16 @@ function notFound(req, res) {
res.sendBody(NOT_FOUND);
res.finish();
}
-
+
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[req.uri.path] || notFound;
-
+ 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]
@@ -33,7 +32,7 @@ var server = createServer(function (req, res) {
res.sendBody(body);
res.finish();
};
-
+
res.simpleJSON = function (code, obj) {
var body = JSON.stringify(obj);
res.sendHeader(code, [ ["Content-Type", "text/json"]
@@ -42,38 +41,37 @@ var server = createServer(function (req, res) {
res.sendBody(body);
res.finish();
};
-
+
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 + "...");
- //var promise = process.fs.cat(filename, encoding);
- var promise = posix.cat(filename,encoding);
-
+ var promise = process.cat(filename, encoding);
+
promise.addCallback(function (data) {
body = data;
headers = [ [ "Content-Type" , content_type ]
@@ -85,12 +83,12 @@ fu.staticHandler = function (filename) {
sys.puts("static file " + filename + " loaded");
callback();
});
-
+
promise.addErrback(function () {
sys.puts("Error loading " + filename);
});
}
-
+
return function (req, res) {
loadResponseData(function () {
res.sendHeader(200, headers);
@@ -99,7 +97,7 @@ fu.staticHandler = function (filename) {
});
}
};
-
+
// stolen from jack- thanks
fu.mime = {
// returns MIME type for extension, or fallback, or octet-steam
@@ -274,4 +272,4 @@ fu.mime = {
, ".yml" : "text/yaml"
, ".zip" : "application/zip"
}
-};
+};
View
9 pollDataServer.js
@@ -2,6 +2,7 @@ var fu = require("./lib/fu");
var sys = require('sys');
process.mixin(GLOBAL, require("./lib/underscore"));
var lpb = require("./lib/longpollingbuffer");
+var url = require("url");
HOST = null; // localhost
PORT = 8000;
@@ -28,11 +29,13 @@ iostat.addListener("output", function (data) {
//Setup the updater page for long polling
fu.get("/update", function (req, res) {
res.sendHeader(200,{"Content-Type": "text/html"});
- var thesince = parseInt(req.uri.params.since);
- if(!thesince){
+ var thesince;
+ if(url.parse(req.url,true).hasOwnProperty('query') && url.parse(req.url,true).query.hasOwnProperty('since')){
+ thesince = parseInt(url.parse(req.url,true)['query']['since']);
+ }
+ else {
thesince = -1;
}
-
rb.addListenerForUpdateSince(thesince, function(data){
var body = '['+_.map(data,JSON.stringify).join(',\n')+']';
res.sendBody( body );

0 comments on commit aa9442e

Please sign in to comment.
Something went wrong with that request. Please try again.