Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use a javascript library on the server side of a NodeJS app when it was designed to run on the client?

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:

  1. Since a NodeJS web app is built on javascript, is it fair to say I can run any non-gui javascript library on the server side?
  2. Can anyone offer some guidance on how I can add that jsrepl library to my Express 3.0 app in a way that allows me to use it in the same way that I would use it on the client side in a browser? Do I have to modify the jsrepl code and add "exports." to the methods I want to use?

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.

like image 712
BeachRunnerFred Avatar asked May 11 '13 19:05

BeachRunnerFred


1 Answers

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)
  • clone down the jsrepl repo git clone git://github.com/replit/jsrepl.git
  • cd into jsrepl and initialize submodules git submodule update --init --recursive
  • still in the folder, run npm install coffee-script and npm install uglify-js, dependencies that are not mentioned anywhere in the repo (ugh).
  • make sure java is installed and run 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.
  • run npm install jsdom, then we can start writing an example file

Here'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 : )

like image 183
Jeff Escalante Avatar answered Oct 31 '22 19:10

Jeff Escalante