NodeJs环境变量与配置文件 [英] NodeJs Environment variables vs config file

查看:614
本文介绍了NodeJs环境变量与配置文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

实际上,我有一个nodejs express应用程序,其配置文件用于诸如主机,端口,JWT令牌,DB参数等参数.

Actually I have a nodejs express app with its config file for params like host, port, JWT token, DB params and more.

问题是,是否有可能将这些参数直接保留在环境变量中(删除任何配置文件)并访问它们,而无需对所有组件和模块进行要求"配置.

The question is if it could have sense to keep those params directly on environment variables (whitout any config file) and acces them without the need of do the "require" for config in all components and modules.

我看到的所有示例都使用一个配置文件,可能与安全性或内存有关?

All examples I see uses a config file, probably something about security or memory?

推荐答案

配置文件可让您非常快速地设置计算机的整个环境-例如S3存储桶,API URL,访问密钥等.如果将它们分开process.env.VARIABLE,那么您将需要设置其中的每一个...您可能会为其创建脚本...现在又有了一个环境文件!

A config file lets your very quickly set the entire environment of a machine - eg S3 buckets, API urls, access keys, etc. If you separate these into separate process.env.VARIABLE then you would need to set each of these...for which you would likely make a script...and now you have an environment file again!

要访问环境变量,您可以在nodejs代码中使用process.env.VARIABLE(始终是字符串),只要在启动过程之前就已设置了变量即可.

To access environment variables you can use process.env.VARIABLE in your nodejs code (is always a string), as long as the variable is set before the process is started.

这篇关于NodeJs环境变量与配置文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆