简体   繁体   中英

Node.js best way to store variable to be used between multiple modules

I have following Node.js app structure:
- main.js - main file
- ./requests/requestSetName.js - different functions sending different requests to API, so I have eg userRequests.js , vechicleRequests.js etc.

First thing I do in main.js is I send a request with login credentials to get access token. For all other requests I need to put this token in the request header. So each function in all modules in request folder needs to have access to it.

What is the best way to store this token and to be able to use it across different modules in Node.js ?

我认为您应该为每个请求文件使用类,并注入另一个包含令牌的类(Globals),在每个请求类的构造函数中,打字稿应使它整洁。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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