是否有标准方法要求 Node 模块位于某个 URL(而不是本地文件系统上)?
就像是:
require('http://example.com/nodejsmodules/myModule.js');
目前,我只是将文件提取到临时文件中,并要求这样做。
您可以使用获取模块http.get方法并在沙箱中执行它使用vm模块方法在此上下文中运行 and 在新上下文中运行.
Example
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');
}));
});
IMO,在没有其他选择的情况下,在服务器应用程序运行时内执行远程代码可能是合理的。并且仅当您信任远程服务和之间的网络时。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)