Created
November 15, 2010 18:59
-
-
Save ericmoritz/700781 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var httpProxy = require("http-proxy"); | |
var http = require("http"); | |
var render = require("./render").render; | |
var XMLHttpRequest = require("./XMLHttpRequest").XMLHttpRequest; | |
function GET(url, callback) { | |
var xhr = new XMLHttpRequest(); | |
xhr.open("GET", url); | |
xhr.onreadystatechange = function(e) { | |
if(xhr.readyState == 4) { | |
callback(xhr); | |
} | |
}; | |
xhr.send(null); | |
} | |
function doESJQ(res, url) { | |
console.log(url); | |
GET(url, function(xhr) { | |
var esjq = JSON.parse(xhr.responseText); | |
GET(esjq.template, function(xhr) { | |
var source = xhr.responseText; | |
GET(esjq.script, function(xhr) { | |
var code = xhr.responseText; | |
render(source, code, function(doc) { | |
res.writeHead(200, {'Content-Type': 'text/plain'}); | |
res.write(doc.innerHTML); | |
res.end(); | |
}); | |
}); | |
}); | |
}); | |
} | |
httpProxy.createServer(function(req, res, proxy){ | |
// Create an error handler so we can use it temporarily | |
var error = function (err) { | |
res.writeHead(200, {'Content-Type': 'text/plain'}); | |
if(req.method !== 'HEAD') { | |
res.write('An error has occurred: ' + sys.puts(JSON.stringify(err))); | |
} | |
res.end(); | |
}; | |
if(/.+\.esjq\.js$/.test(req.url)) { | |
var full_uri = "http://localhost" + req.url; | |
try { | |
doESJQ(res, full_uri); | |
} catch(e) { | |
error(e); | |
} | |
} else { | |
proxy.proxyRequest(80, 'localhost'); | |
} | |
}).listen(10002); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var jsdom = require("jsdom"); | |
function render(template, code, callback) { | |
var window = jsdom.jsdom(source).createWindow(); | |
jsdom.jQueryify(window, __dirname + "/jquery-1.4.2.js", | |
function() { | |
var jQuery = window.jQuery; | |
// This is probably pretty dangerous in node.js | |
eval(code); | |
callback(window.document); | |
}) | |
} | |
exports.render = render; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"template":"http://localhost/test.html", | |
"script":"http://localhost/test.js" | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!doctype html> | |
<p>My wife's name is <span id="spouse" /></p> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
jQuery("#spouse").html("Gina Moritz"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment