如果设计为在客户端上运行,我如何在NodeJS应用程序的服务器端使用javascript库?

时间:2021-07-03 04:33:39

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...

我正在深入研究NodeJS和Express(对我来说太复杂了)来构建一个实时的Web应用程序。目前,我正在尝试了解如何在服务器端使用现有的JavaScript库。问题是库似乎设计为在客户端运行,因此,说明只显示如何在客户端使用它。我在说的图书馆可以在这里找到......

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. 由于NodeJS Web应用程序是基于javascript构建的,可以说我可以在服务器端运行任何非gui javascript库吗?

  3. 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?
  4. 任何人都可以提供一些指导,说明我如何将这个jsrepl库添加到我的Express 3.0应用程序中的方式允许我以与在浏览器中客户端使用它相同的方式使用它?我是否必须修改jsrepl代码并添加“exports”。我想用的方法?

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.

提前感谢您的所有智慧!我正在尽我所能去理解这一切。

3 个解决方案

#1


9  

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.

所以这是可能的,但你需要一些严肃的hackery来使它工作。由于这不是一个节点模块,而是像其他人注意到的那样从浏览器中编写,因此您需要在节点内使用DOM来执行它。幸运的是,我们有一个很棒的jsdom项目,可以让我们做到这一点。所以让我们设置这个东西吧。

  • cd into your node project (create one if there isn't one already)
  • cd进入你的节点项目(如果没有一个,则创建一个)

  • clone down the jsrepl repo git clone git://github.com/replit/jsrepl.git
  • 克隆jsrepl repo git clone git://github.com/replit/jsrepl.git

  • cd into jsrepl and initialize submodules git submodule update --init --recursive
  • cd到jsrepl并初始化子模块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).
  • 仍然在文件夹中,运行npm install coffee-script和npm install uglify-js,在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.
  • 确保安装了java并运行cake bake。经过漫长的编译java文件的过程后,命令将完成,jsrepl将被构建并准备就绪。

  • run npm install jsdom, then we can start writing an example file
  • 运行npm install jsdom,然后我们就可以开始编写一个示例文件了

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

这是将JSREPL引入其工作所需的最少代码量。我们在这里所做的只是需要jsdom并实例化它,直接从文件中读取jsrepl源代码。如果你用节点文件名运行这个文件,它将注销你的JSREPL对象,它可以像在浏览器中一样使用:)

#2


2  

You can run phantomjs in Node, which is a headless webkit browser. Then run jsrepl inside of phantomjs.

您可以在Node中运行phantomjs,这是一个无头webkit浏览器。然后在phantomjs中运行jsrepl。

#3


1  

  1. No. There are things on the client side that you don't have on the server side (and vice-versa): for instance, the DOM.
  2. 没有。客户端有些东西在服务器端没有(反之亦然):例如,DOM。

  3. I've never worked with jsrepl myself, but assuming that it's platform-agnostic, require()ing it from a node module should be OK. However, there seem to be some DOM-specific things in the scripts in question (e.g. document.getElementById) that suggest otherwise.
  4. 我自己从未使用过jsrepl,但假设它与平台无关,那么从节点模块中对它进行require()应该没问题。但是,在相关脚本中似乎存在一些特定于DOM的内容(例如document.getElementById)。

#1


9  

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.

所以这是可能的,但你需要一些严肃的hackery来使它工作。由于这不是一个节点模块,而是像其他人注意到的那样从浏览器中编写,因此您需要在节点内使用DOM来执行它。幸运的是,我们有一个很棒的jsdom项目,可以让我们做到这一点。所以让我们设置这个东西吧。

  • cd into your node project (create one if there isn't one already)
  • cd进入你的节点项目(如果没有一个,则创建一个)

  • clone down the jsrepl repo git clone git://github.com/replit/jsrepl.git
  • 克隆jsrepl repo git clone git://github.com/replit/jsrepl.git

  • cd into jsrepl and initialize submodules git submodule update --init --recursive
  • cd到jsrepl并初始化子模块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).
  • 仍然在文件夹中,运行npm install coffee-script和npm install uglify-js,在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.
  • 确保安装了java并运行cake bake。经过漫长的编译java文件的过程后,命令将完成,jsrepl将被构建并准备就绪。

  • run npm install jsdom, then we can start writing an example file
  • 运行npm install jsdom,然后我们就可以开始编写一个示例文件了

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

这是将JSREPL引入其工作所需的最少代码量。我们在这里所做的只是需要jsdom并实例化它,直接从文件中读取jsrepl源代码。如果你用节点文件名运行这个文件,它将注销你的JSREPL对象,它可以像在浏览器中一样使用:)

#2


2  

You can run phantomjs in Node, which is a headless webkit browser. Then run jsrepl inside of phantomjs.

您可以在Node中运行phantomjs,这是一个无头webkit浏览器。然后在phantomjs中运行jsrepl。

#3


1  

  1. No. There are things on the client side that you don't have on the server side (and vice-versa): for instance, the DOM.
  2. 没有。客户端有些东西在服务器端没有(反之亦然):例如,DOM。

  3. I've never worked with jsrepl myself, but assuming that it's platform-agnostic, require()ing it from a node module should be OK. However, there seem to be some DOM-specific things in the scripts in question (e.g. document.getElementById) that suggest otherwise.
  4. 我自己从未使用过jsrepl,但假设它与平台无关,那么从节点模块中对它进行require()应该没问题。但是,在相关脚本中似乎存在一些特定于DOM的内容(例如document.getElementById)。