我真的被困在这里。尽管mod_lua中的模块require似乎可以与lua解释器正常工作,但mod_v8中的require()似乎“包括”了整个脚本。我还没有找到一种使作品仅导入脚本中需要的模块(而不是节点模块)的方法。
例如,在脚本中,我有以下内容:
//core/dtmf_1.js
const a = (arg) => { return arg * 2 }
const b = (arg) => { return arg / 2 }
//I get an error DONT WORKS
exports.a = a
exports.b = b
下面的示例对我也不起作用,但不会引发错误。
//core/dtmf_2.js
export function a = (arg) => { return arg * 2 }
export function b = (arg) => { return arg / 2 }
否则,当我打电话
//ivr.js
import a from 'core/dtmf_2.js'
我在“导入”中遇到错误
但是如果我只是这样做:
//core/dtmf_3.js
const function a = (arg) => { return arg * 2 }
const function b = (arg) => { return arg / 2 }
//ivr.js
require('core/dtmf_3.js')
console.log(b(30)) <-- WORKS! Outputs 15
我想知道mod_v8的conf中是否有一些设置允许导入模块。之所以要这样做,是因为我在库中预定义了不同的方法,但是我很少按服务使用多个方法。感谢您的建议。
FreeSwitch使用V8,而不使用Node.js,因此您将无法获得require
和export
函数。对于包含JavaScript脚本,您可以使用FreeSwitch include
或require
关键字-都是一样的。 Include
将包含整个脚本,因此您无法执行以下示例,因为您两次加载math.js,因此会发生异常:SyntaxError: Identifier 'pi' has already been declared (near: "const pi = 3.14;")
math.js
const pi = 3.14;
function circleArea(radius){
return radius * radius * pi;
}
function sum(a, b){
return a + b;
}
circle.js
include('/etc/freeswitch/test/math.js');
function getArea(radius){
return circleArea(radius);
}
main.js
include('/etc/freeswitch/test/circle.js');
include('/etc/freeswitch/test/math.js');
//math.js
log( sum(5,10) );
//circle.js
log( getArea(10) );
include
实现使您可以将脚本中的代码“复制-粘贴”到首先调用include
的脚本中,以便您可以利用该代码将代码加载到变量中。例如:
math.js
(a, b) => {
return a + b;
}
main.js
const sum = include('/etc/freeswitch/test/math.js');
//logs 15
log( sum(5, 10) );
现在,将相同的脚本加载到全局范围中不会有问题。我们可以使用自我调用功能将其提升到另一个层次。
math.js
(() => {
const pi = 3.14;
function circleArea(radius){
return radius * radius * pi;
}
function sum(a, b){
return a + b;
}
return { circleArea, sum };
})();
main.js
const math = include('/etc/freeswitch/test/math.js');
const { sum, circleArea } = include('/etc/freeswitch/test/math.js');
//with module
log( math.circleArea(5) ); // logs 78.5
log( math.sum(5, 10) ); // logs 15
//direct call
log( circleArea(5) ); // logs 78.5
log( sum(5, 10) ); // logs 15
但是,每次您要加载math.js时,它都将充当其自己的模块,并且将加载多次,而不是像Node.js模块那样。例如:
FreeSwitch
math.js
(() => {
log("Math loaded...");
})();
main.js
const math1 = include('/etc/freeswitch/test/math.js');
const math2 = include('/etc/freeswitch/test/math.js');
这将两次打印“ Math loading ...”。
Node.js
math.js
(() => {
console.log("Math loaded...");
})();
main.js
const math1 = require('./math.js');
const math2 = require('./math.js');
这一次打印“ Math loading ...”。
从这里您可以编写自己的include脚本(这仍然是一种不好的方法,但是应该可以使用)。像这样的东西:
include.js
if (typeof _include === 'undefined') {
var _loadedScripts = {};
function _include(script){
if (!_loadedScripts.hasOwnProperty(script)){
_loadedScripts[script] = include(script);
}
return _loadedScripts[script];
}
}
然后您可以像以前一样编写模块:
math.js
(() => {
log("Math loaded...");
var pi = 3.14;
return { pi };
})();
并且当您要包括脚本时:
main.js
include('/etc/freeswitch/include.js');
const math1 = _include('/etc/freeswitch/test/math.js');
const math2 = _include('/etc/freeswitch/test/math.js');
math1.pi = 10;
math2.pi = 20;
log(math1.pi);
log(math2.pi);
这将产生:
Math loaded...
20
20