我有一个 node.js 控制器,它创建了一个 csv 文件,它存储在一个特定的文件夹中,还有一个来自称为端点的 var 的值。
问题是在控制器行的末尾我有这个 start 函数来启动另一个名为 bot.js 的 javascript 文件中的另一个进程
const scrapeWebsite = require('../bot')
const start = new scrapeWebsite();
var endpoint ="192.186.1.1"
try {
start.init();
} catch (e) {
res.send(JSON.stringify(e, null, 5))
}
在 bot.js 文件中,我想访问我的 controller.js 的端点变量的值,以便在 bot 函数过程中使用它,但我不知道该怎么做。
像在 bot.js 中声明一个与 controller.js 中的端点具有相同值的新变量
class scrapeWebsite {
init() {
this.startScrape()
}
async startScrape() {...}
答案 0 :(得分:2)
从 bot.js 导出该函数
module.exports.function_name = function_name
当你想在 node.js 控制器中运行它时,使用
导入它
const {function_name} = require("../bot")
当你想执行那个函数时,调用那个函数
function_name(<with, arguments, from, bot.js, file>)