Skip to content

Instantly share code, notes, and snippets.

@ericmoritz
Created November 15, 2010 18:59
Show Gist options
  • Save ericmoritz/700781 to your computer and use it in GitHub Desktop.
Save ericmoritz/700781 to your computer and use it in GitHub Desktop.
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);
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;
{
"template":"http://localhost/test.html",
"script":"http://localhost/test.js"
}
<!doctype html>
<p>My wife's name is <span id="spouse" /></p>
jQuery("#spouse").html("Gina Moritz");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment