简体   繁体   English

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

[英]Node.JS Adding access Node Modules using Require() to a CreateContext()

In my application I CreateContext and then RunInContext. 在我的应用程序中,我依次创建CreateContext和RunInContext。 I need to add access to only certain Node modules within the Context. 我只需要添加对上下文中某些Node模块的访问权限。 I know how to add my own Javascript methods, but get errors when I add Node modules like async and http. 我知道如何添加自己的Javascript方法,但是在添加诸如异步和http之类的Node模块时会出错。 How can I do this? 我怎样才能做到这一点?

I am using the Sandbox module https://github.com/gf3/sandbox to run child processes 我正在使用沙盒模块https://github.com/gf3/sandbox运行子进程

Code

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 returns the module, so if you do not assign it to something it will not be available. 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);

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

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