简体   繁体   English

如何在节点js中永远设置一个值

[英]how to set a value forever in node js

I'm new in node js.我是节点 js 的新手。 I want to save an id in a global variable.我想在全局变量中保存一个 id。 I need that in many functions.我在许多功能中都需要它。 But when server restart the variable get empty.但是当服务器重新启动时,变量变空。 Is there any way to save my required value for a long time even when server restart?即使服务器重新启动,有没有办法长时间保存我所需的值?

You can store variables as environmental variables, so that they can load each time you restart your server.您可以将变量存储为环境变量,以便在您每次重新启动服务器时加载它们。 or you can have them in file system or in database.或者您可以将它们放在文件系统或数据库中。

  1. Environment variables can be set command line as node.js process variable and read them anywhere in you node code with process.env.[variable_name]环境变量可以在命令行中设置为node.js 进程变量,并使用 process.env.[variable_name] 在节点代码中的任何位置读取它们
  2. You can write your variables in file and read them using FS module您可以将变量写入文件并使用FS 模块读取它们
  3. Or you can have you variables in database as well.或者您也可以在数据库中使用变量。

You can save the variable using Store-js您可以使用Store-js保存变量

To save the value using store js npm install store-js;使用 store js npm install store-js 保存值;

 import store from 'store-js'; store.set('variable', value);//for saving value store.get('variable');// for getting of the value

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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