I'm diving into NodeJS and Express (it's sooo complicated to me) to build a real-time web app. At the moment, I'm trying to understand how I can use an existing javascript library on the server side. The problem is the library appears to be designed to run on the client side and, as a result, the instructions only show you how to use it on the client side. The library I'm talking about can be found here...
https://github.com/replit/jsrepl
Questions:
Meaning, on the server side, I can execute the following code...
var jsrepl = new JSREPL({
input: inputCallback,
output: outputCallback,
result: resultCallback,
error: errorCallback,
progress: progressCallback,
timeout: {
time: 30000,
callback: timeoutCallback
}
});
Thanks in advance for all your wisdom! I'm doing my best to understand all this.
So this is possible, but you need some serious hackery in order to get it working. Since this is not a node module and is written from the browser as others have noted, you need a DOM within node to execute it into. Luckily, we have the wonderful jsdom project which allows us to do just that. So let's get this thing set up.
cd
into your node project (create one if there isn't one already)git clone git://github.com/replit/jsrepl.git
jsrepl
and initialize submodules git submodule update --init --recursive
npm install coffee-script
and npm install uglify-js
, dependencies that are not mentioned anywhere in the repo (ugh).cake bake
. After a lengthy process of compiling java files and such the command will finish and jsrepl will be built and ready to go.npm install jsdom
, then we can start writing an example fileHere's a minimal example:
var jsdom = require('jsdom'),
fs = require('fs'),
jsrepl = fs.readFileSync('./jsrepl/repl.js', 'utf8');
jsdom.env({
html: "<script src='jsrepl.js' id='jsrepl-script'></script> ",
src: [jsrepl],
done: function(err, window){
if (err) return console.error(err);
run_jsrepl.call(window);
}
});
function run_jsrepl(){
console.log(this.JSREPL)
}
Here's the minimum amount of code required to get JSREPL
into a place where it's working. All we're doing here is requiring jsdom and instantiating it, reading in the jsrepl source straight from the file. If you run this file with node filename
it will log out your JSREPL
object, which can be used just like it's in the browser : )
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With