简体   繁体   English

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

[英]Loading remote js file using require with node.js

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.似乎出于某种技术原因,运行我的代码的进程在另一台机器上,然后我可以访问(用于文件上传等),所以服务器场上的人要求我更改我的代码,以便我将从全局位置而不是本地位置加载“forza4.js”中的代码,就像我现在所做的那样。 So I changed the code to this:所以我把代码改成这样:

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.尤瓦尔。

PS This is a follow up to this thread: This is the older and resolved post PS 这是此线程的后续: 这是较旧且已解决的帖子

Take a look at the node.js modules docs查看 node.js模块文档

Specifically, refer to the require algorithm具体参考require算法

Within node.js, require calls 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 request and run it with the built-in vm module - 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.当然这不是最好的解决方案,但它是一个解决方案。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM