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

I dive into NodeJS and Express (this is difficult for me) to create a real-time web application. At the moment, I am trying to understand how I can use the existing server-side javascript library. The problem is that the library seems to be designed to work on the client side, and as a result, the instructions show how to use it on the client side. The library I'm talking about can be found here ...

https://github.com/replit/jsrepl

Questions:

  • Since the NodeJS web application is built on javascript, can it be said that I can run any javascript library without gui on the server side?
  • Can anyone offer some recommendations on how I can add this jsrepl library to an Express 3.0 application so that I can use it the same way as using it on the client side in a browser? I need to change jsrepl code and add "export". to the methods I want to use?

Value, 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 } }); 

Thank you for your wisdom! I am working hard to understand all this.

+7
source share
3 answers

So itโ€™s possible, but you need serious hacking to make it work. Since this is not a node module and is written in a browser, as others have noted, it requires the DOM inside the node to execute it. Fortunately, we have a wonderful jsdom project that allows us to do just that. So let it tune.

  • cd into your node project (create it if it is not already)
  • clone jsrepl repo git clone git://github.com/replit/jsrepl.git
  • cd in 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 running cake bake . After a lengthy process of compiling java files and such a command will complete, 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 is the minimum amount of code needed to get jsrepl to its place of work. All we do here is jsdom requirement and its creation, reading in jsrepl source directly from the file. If you run this file using node filename , it will exit your jsrepl object, which can be used in the same way as in the browser :)

+8
source

You can run phantomjs in Node, which is the headless web whale browser. Then run jsrepl inside the phantoms.

+2
source
  • Not. On the client side, there are things that you do not have on the server side (and vice versa): for example, the DOM.
  • I have never worked with jsrepl myself, but assuming it is agnostic, require() from it with the node module should be fine. However, there seem to be some DOM-specific things in the scenarios in question (e.g. document.getElementById ) that offer otherwise.
+1
source

All Articles