Javascript 验证我的 node.js 实例是开发还是生产
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10694571/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Verify if my node.js instance is dev or production
提问by guiomie
Right now, whenever I want to deploy a node.js server to my production server, I need to change all the IP/DNS/username/password for my various connection to my databases and external APIs.
现在,每当我想将 node.js 服务器部署到我的生产服务器时,我都需要更改与数据库和外部 API 的各种连接的所有 IP/DNS/用户名/密码。
This process is annoying, is there a way to verify if the currently running node.js instance is in cloud9ide or actually my production joyent smartmachine?
这个过程很烦人,有没有办法验证当前运行的node.js实例是在cloud9ide还是我的生产joyent smartmachine?
If I am able to detemrine (in my running code) on which server my node.js instance is running , I'll add a condition that set the values to the prod or dev.
如果我能够确定(在我的运行代码中)我的 node.js 实例在哪个服务器上运行,我将添加一个条件,将值设置为 prod 或 dev。
Thank you
谢谢
回答by alessioalex
Normally you should run a node app in production like this:
通常你应该像这样在生产中运行一个节点应用程序:
NODE_ENV=production node app.js
NODE_ENV=production node app.js
Applications with Express, Socket.IO and other use process.env.NODE_ENV
to figure out the environment.
带有 Express、Socket.IO 等的应用程序使用process.env.NODE_ENV
来搞清楚环境。
In development you can omit that and just run the app normally with node app.js
.
在开发中,您可以省略它,只需使用node app.js
.
You can detect the environment in your code like this:
您可以像这样检测代码中的环境:
var env = process.env.NODE_ENV || 'development';
loadConfigFile(env + '.json', doStuff);
Resources:
资源:
回答by Vadim Baryshev
I think the easiest way to set the environment is to pass command-line argument to your application.
我认为设置环境的最简单方法是将命令行参数传递给您的应用程序。
node ./server.js dev
In your script you need to handle this argument and set configuration what you need for it.
在你的脚本中,你需要处理这个参数并设置你需要的配置。
var env = process.argv[2] || 'dev';
switch (env) {
case 'dev':
// Setup development config
break;
case 'prod':
// Setup production config
break;
}
Also, i was created modulethat makes the configuration process a bit easier. Maybe it will help you.
此外,我创建了模块,使配置过程更容易一些。也许它会帮助你。
回答by Slava Fomin II
Actually, I would not recommend to store configuration values like database connection information, passwords, access tokens and such inside of actual application code for the following reasons:
实际上,我不建议在实际应用程序代码中存储数据库连接信息、密码、访问令牌等配置值,原因如下:
Hardcoding those values make it difficult to change them later on. You will have to release a new version of the application to change those values.
This is a serious security violation, because production-grade configuration data and passwords shouldn't be stored in code. It's very easy to leak this sensitive data.
对这些值进行硬编码使得以后很难更改它们。您必须发布应用程序的新版本才能更改这些值。
这是严重的安全违规,因为生产级配置数据和密码不应存储在代码中。泄露这些敏感数据非常容易。
The better approach would be to externalize this data and pass it to your application during execution. This is normally done by means of environment variables. You just need to define unique environment variable for each peace of data that needs to be changeable between different environments.
更好的方法是将这些数据外部化并在执行期间将其传递给您的应用程序。这通常是通过环境变量来完成的。您只需要为需要在不同环境之间更改的每个和平数据定义唯一的环境变量。
For example: DB_HOST
, DB_USER
, DB_PASSWORD
. Then you could pass those values to you app in production this way:
例如:DB_HOST
、DB_USER
、DB_PASSWORD
。然后,您可以通过以下方式将这些值传递给生产中的应用程序:
$ NODE_ENV=production DB_HOST=1.2.3.4 DB_USER=someusername DB_PASSWORD=somerandompassword /bin/node app.js
Actually, this values could be encrypted and added to the codebase and then decrypted during the deployment. However, make sure that decryption key is stored securely in deployment system or provided interactively by the release engineer. Shippable allows to do thisout of the box.
实际上,这些值可以加密并添加到代码库中,然后在部署期间解密。但是,请确保解密密钥安全地存储在部署系统中或由发布工程师交互提供。Shippable 允许开箱即用。
In the development environment it gets simpler, because you can use very convenient dotenv
module. Just create a .env
file in your project's root directory and add all variables to it:
在开发环境中它变得更简单,因为您可以使用非常方便的dotenv
module。只需.env
在项目的根目录中创建一个文件并将所有变量添加到其中:
DB_HOST=1.2.3.4
DB_USER=someusername
DB_PASSWORD=somerandompassword
But, make sure to exclude it from you VCS, because each developer probably would want to have personal configuration. You can create a .env.dist
file to contain default configuration, which later could be used as a template: cp .env.dist .env
.
但是,请确保将其从您的 VCS 中排除,因为每个开发人员可能都希望拥有个人配置。您可以创建一个.env.dist
文件来包含默认配置,稍后可以将其用作模板:cp .env.dist .env
.
Also, I've implemented a reusable module, which allows to automatically detect environment by analyzing both CLI arguments and NODE_ENV variable. This could be useful on your development machine, because you can easily change environment by passing a CLI argument to you Node.js program like this: $ node app.js --prod
. It's also nice to use with Gulp: $ gulp build --prod
.
此外,我还实现了一个可重用模块,它允许通过分析 CLI 参数和 NODE_ENV 变量来自动检测环境。这可能是在开发机器上是有用的,因为你可以通过一个命令行参数传递给你Node.js加载这样的程序轻易改变环境:$ node app.js --prod
。与 Gulp 一起使用也很好:$ gulp build --prod
。
Please see more details and use cases on the detect-environment
's page.
请在detect-environment
的页面上查看更多详细信息和用例。
回答by ramachandrareddy reddam
Npm config Module is very useful for Environment based setup and running app based on it. please check below link
Npm config 模块对于基于环境的设置和基于它运行的应用程序非常有用。请检查以下链接