[英]How to share an instance variable between files in Node.JS?
我正在嘗試構建一個Telegram機器人,我正在使用以下npm軟件包: https ://www.npmjs.com/package/node-telegram-bot-api
我有以下項目結構:
在我的app.js
文件中,我做了: const bot = new TelegramBot(token, {polling: true});
。 我想與index.js
文件共享此bot
實例,以便在index.js
文件中可以執行以下操作:
bot.onText(/\/time (.+)/, (msg, match) => {
});
我不確定如何完成此操作,唯一想到的方法是在app.js文件上使用module.exports,但我不確定這是否是正確的方法。
-編輯-
在app.js文件中,我要執行以下操作:
require('./plugins/time')
require('./plugins/weather')
然后輸入node app.js
運行該機器人。
是的,這是正確的方法。 通常的。
也有全局變量,但是它們是邪惡的,因此您不應該使用它們。
只需通過導出導出變量即可。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.