javascript 使用 node.js 的 require 加载远程 js 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23566310/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 01:06:42  来源:igfitidea点击:

Loading remote js file using require with node.js

javascriptnode.js

提问by Yuval

I've been working on an online socket server using NodeJS and javascript, and I've been creating "playrooms" in my code using require:

我一直在使用 NodeJS 和 javascript 开发在线套接字服务器,并且我一直在使用 require 在我的代码中创建“游戏室”:

new_game_obj = require('./forza4.js');

Now.. this works find when I test my code on my local machine, but for the production server I've been presented with a problem. It would seem that for some technical reason, the process that runs my code is on a different machine then the one I have access to (for file uploading, and such), so I was asked by the guys on the server farm to change my code so that I will load the code I have in "forza4.js" from a global position, and not local, like I do at the moment. So I changed the code to this:

现在.. 当我在本地机器上测试我的代码时,这可以找到,但是对于生产服务器,我遇到了一个问题。似乎出于某种技术原因,运行我的代码的进程在另一台机器上,然后我可以访问(用于文件上传等),所以服务器场上的人要求我更改我的代码,以便我将从全局位置而不是本地位置加载“forza4.js”中的代码,就像我现在所做的那样。所以我把代码改成这样:

new_game_obj = require('http://www.xxxxx.com/blabla/forza4.js');

(Of course I tested to see if the file is there, just to be sure, it shows in the browser when I point to that actual address) But I get an error from my code (again, at this point, I'm running this locally on my machine), which says:

(当然,我测试了文件是否存在,只是为了确定,当我指向该实际地址时,它会显示在浏览器中)但是我的代码出现错误(同样,此时,我正在运行这在我的机器上本地),它说:

Exception: Error: Cannot find module 'http://www.xxxxx.com/blabla/forza4.js'

异常:错误:找不到模块“ http://www.xxxxx.com/blabla/forza4.js

So just to be on the safe side, I did:

所以为了安全起见,我做了:

new_game_obj = require('http://92.xx.xx.xx/blabla/forza4.js'); 

But again, the same error.

但是,同样的错误。

Should there be a problem loading an "extension" to my code from a remote server, or am I just formatting the "require" call wrong?

从远程服务器向我的代码加载“扩展”是否应该出现问题,或者我只是格式化“require”调用错误?

Thanks a bunch!

谢谢一堆!

Yuval.

尤瓦尔。

P.S. This is a follow up to this thread: This is the older and resolved post

PS 这是此线程的后续: 这是较旧且已解决的帖子

回答by laconbass

Take a look at the node.js modules docs

查看 node.js模块文档

Specifically, refer to the require algorithm

具体参考require算法

Within node.js, requirecalls are synchronous, so it's not possible to load files that are not on your file system (ie, from an external url).

在 node.js 中,require调用是同步的,因此无法加载不在您的文件系统上的文件(即,来自外部 url)。

Update

更新

You could fetch the code through an http request- or, even better, an https requestand run it with the built-in vmmodule - or even with eval, but that seems not a good idea - as suggested on this old question.

您可以通过http 请求获取代码- 或者更好的是,通过https 请求获取代码并使用内置vm模块运行它- 甚至使用eval,但这似乎不是一个好主意 - 正如这个老问题所建议的那样。

Something like

就像是

https.get( 'https://www.xxxxx.com/blabla/forza4.js', function( res ){
  res.on( 'data', function( data ){
    vm.runInThisContext( data, 'remote/forza4.js' );
  });
});

Note: I did not test this code

注意:我没有测试这段代码

Sure it isn't the best solution, but is a solution.

当然这不是最好的解决方案,但它是一个解决方案。

回答by Ian Grainger

How about the 'require from URL' NPM package?

'require from URL' NPM 包怎么样?

Just found it - not tried it, yet! https://www.npmjs.com/package/require-from-url

刚刚找到它 - 还没有尝试过!https://www.npmjs.com/package/require-from-url