Node runner for continuously evaluating POSTed js code from collabedit
-
The node server just listens for
text/plain
JavaScript code that is sent to it through an HTTP POST and writes it tocode.js
. CORS allows these to be sent across domains so that this can work from any origin. -
A
grunt watch
task listens for changes oncode.js
, runs that fine, and pipes&>
toout.log
. -
server.js
watchesout.log
for changes and uses socket.io to emit the contents toindex.html
which simply dynamically updates it in a<pre>
block.
Run the server locally: node server.js
.
Separately start the grunt watch
task to run the code everytime it detects changes.
I personally run a localtunnel so that whoever I'm collaborating with can also see the output as well.
Then in collabedit, or some equivalent, collect the code and dispatch it like so:
var start, end, baseurl;
dispatch = function (code) {
var xhr = new XMLHttpRequest();
xhr.open('POST', baseurl + '/' + (start ? ('?start='+start+(end ? ('&end='+end) : '')) : ''), true);
xhr.setRequestHeader('Content-Type', 'text/plain');
xhr.onreadystatechange = function () {console.dir(arguments);};
xhr.send(code);
};
// Sample for collabedit
document.getElementById("textarea").onkeyup = function () {
dispatch(document.getElementById("content_highlight").textContent);
};
start = 0; // first line of desired code (1-indexed)
end = 0; // last line of desired code
baseurl = 'http://localhost:3000';