Node.JS使用Require()向CreateContext()添加访问节点模块

在我的应用程序中,我CreateContext,然后RunInContext。 我需要添加对上下文中的某些节点模块的访问。 我知道如何添加自己的Javascript方法,但是当我添加像async和http这样的节点模块时会出错。 我怎样才能做到这一点?

我正在使用沙箱模块https://github.com/gf3/sandbox来运行subprocess

var context = Script.createContext(); context.myOwnFunctions = function() { //my own javascript } context.myNodeFunctions = function() { //require('async') //require('http') /Add some function that use the items I required above } var run = Script.runInContext('code to run', context); 

require返回模块,所以如果你不分配它的东西,它将不可用。

 var context = Script.createContext(); context.myOwnFunctions = function() { //my own javascript } context.myNodeFunctions = function() { this.async = require('async'); this.http = require('http'); //Add some function that use the items I required above } var run = Script.runInContext('code to run', context); 
    Interesting Posts