是否有一种标准方法要求 node 模块位于某个URL(而不是本地文件系统)上?
比如:
require('http://example.com/nodejsmodules/myModule.js');
目前,我只是将文件提取到一个临时文件中,并要求这样做.
是否有一种标准方法要求 node 模块位于某个URL(而不是本地文件系统)上?
比如:
require('http://example.com/nodejsmodules/myModule.js');
目前,我只是将文件提取到一个临时文件中,并要求这样做.
您可以使用http.get方法获取模块,并使用vm模块方法runInThisContext和runInNewContext在沙箱中执行它.
实例
var http = require('http')
, vm = require('vm')
, concat = require('concat-stream'); // this is just a helper to receive the
// http payload in a single callback
// see https://www.npmjs.com/package/concat-stream
http.get({
host: 'example.com',
port: 80,
path: '/hello.js'
},
function(res) {
res.setEncoding('utf8');
res.pipe(concat({ encoding: 'string' }, function(remoteSrc) {
vm.runInThisContext(remoteSrc, 'remote_modules/hello.js');
}));
});
在我看来,在没有替代方案的情况下,在服务器应用程序运行时内执行远程代码可能是合理的.而且只有在你信任远程服务和网络的情况下.