Using functions inside scripts required by NodeJS's requ
Using functions inside scripts required by NodeJS's required()
如何使用Node的require()函数访问"包含"的脚本中的函数?
--main-js.js--
var webnotis = require('./modules/web-notification.js')
--web-notification.js--
function getURL(host, path) {
...
}
此外,我将如何在其他必需的脚本中使用此函数?
--report-tables.js--
var cltvOut;
exports.cltv = function cltv(getURL)
{
clearTimeout(cltvOut);
cltvOut = setTimeout(function(){
if(exports.getURL('192.168.0.15', '/IMS4/Reports/calculateCLTV'))
{
cltv();
} else {
console.log('CLTV error.')
}
}, 2000);
}
webnotis2 = require('./web-notification.js')
var cltvOut;
exports.cltv = function cltv()
{
clearTimeout(cltvOut);
cltvOut = setTimeout(function(){
if(webnotis2.getUrl('192.168.0.15', '/IMS4/Reports/calculateCLTV'))
{
cltv();
} else {
console.log('CLTV error.')
}
}, 2000);
}
如果它不是module.exports
的一部分,则不能。例如:
web通知.js
function getURL(host, path) {
...
}
module.exports = exports = {
getURL: getURL
};
main js.js
var webnotis = require('./modules/web-notification.js');
webnotis.getURL(...);
这叫做导出模块。
这里的样品:
创建一个文件/utils.js,并定义merge()函数,如下所示。。
function merge(obj, other) {
//...
};
exports.merge = merge;
现在,合并函数可以访问utils
中的另一个JS,如下所示:
var utils = require('./utils');
utils.merge();
var webnotis = require('./modules/web-notification.js')
var host='urhost';
var path='urpath';
webnotis.getURL(host,path,function(err,res){
if(!err){
console.log('url is '+res);
}
});
web-notification.js
exports.getURL=function(host, path,cb) {
var url=host+path;
cb(null,url);
}
相关文章:
- nodejs-expressjs上传图像并显示它们
- TypeError:在不兼容的接收器nodejs上调用了方法Uint8Array.length
- NodeJS-readline暂停和恢复事件发射器(逐行读取)
- NodeJS日期格式不起作用
- Nested Q.all nodejs
- Nodejs API控制器,用于在API之间切换
- 否'访问控制允许来源'标头存在于IISNOde中请求的资源(AngularJS+NodeJs)上
- 如何在大型nodeJS代码的基础上逐步引入typescript
- 在Nodejs中Express输入DEBUG=app时权限被拒绝/bin
- 无法使用nodeJS在html中设置Image src
- 与域在同一台计算机上运行的NODEJS服务器的CORS错误
- NodeJS API调用中Array中的Push和Pull元素
- 基于api密钥的NodeJS web服务
- 在gump和nodejs中使用Typescript时,未定义对require和exports的引用
- “util.inherits”和在NodeJS中扩展原型之间的区别
- 将一个方法转换为promise:Nodejs
- nodejs中匿名函数的使用
- Node和Nodejs版本不同
- Nodejs服务器在解析之前生成输入表单
- Using functions inside scripts required by NodeJS's requ