Storing configuration in files instead of the environment has many downsides, including mistakenly checking in the wrong configuration in the wrong environment, coupling configuration with code, and scaling issues in larger server architectures.

We’ll learn how to update app code to look at environment variables for configuration values instead of using configuration files, and different approaches for managing environment variables between dev/stage/prod.

1. Create an .env file and store all the env variables in the file.
// .env

MONGO_URI=mongodb://localhost:27017/foo
2.  Using the env variable automaticlly from dotenv 
// When need to use env variables, require dotenv.
require('dotenv').config();
var MongoClient = require('mongodb').MongoClient; // Then use the variable from process.env
MongoClient.connect(process.env.MONGO_URI, function(err, db) {
if (err) {
console.log('Cannot connect to MongoDB!', err);
} else {
console.log('Connected to MongoDB!');
}
});

[Node.js] Manage Configuration Values with Environment Variables的更多相关文章

  1. [Whole Web, Nods.js, PM2] Passing environment variables to node.js using pm2

    learn how to pass environment variables to your node.js app using the pm2 config file. This is usefu ...

  2. Node.js NPM Tutorial: Create, Publish, Extend & Manage

    A module in Node.js is a logical encapsulation of code in a single unit. It's always a good programm ...

  3. Node Sass could not find a binding for your current environment: OS X 64-bit with Node.js 10.x

    运行Reac项目报: Node Sass could not find a binding for your current environment: OS X 64-bit with Node.js ...

  4. VUE npm run dev 启动时,报了一大堆错误 Node Sass could not find a binding for your current environment: Windows 64-bit with Node.js 7.x

    npm run dev 启动时,报了一大堆错误 Module build failed: Error: Missing binding E:\2017VocaSchool\vocationWeb\no ...

  5. A chatroom for all! Part 1 - Introduction to Node.js(转发)

    项目组用到了 Node.js,发现下面这篇文章不错.转发一下.原文地址:<原文>. ------------------------------------------- A chatro ...

  6. Node.js高级编程读书笔记 - 2 文件和进程处理

    Outline 3 文件.进程.流和网络 3.1 查询和读写文件 3.2 创建和控制外部进程 3.3 读写数据流 3 文件.进程.流和网络 3.1 查询和读写文件 path 从Node 0.8起,pa ...

  7. [转]Getting Start With Node.JS Tools For Visual Studio

    本文转自:http://www.c-sharpcorner.com/UploadFile/g_arora/getting-started-with-node-js-tools-for-visual-s ...

  8. [转]Node.js tutorial in Visual Studio Code

    本文转自:https://code.visualstudio.com/docs/nodejs/nodejs-tutorial Node.js tutorial in Visual Studio Cod ...

  9. Windows 7 下 Node.js 连接 Oracle

    原创作者: sailtseng 1. 安装 Oracle 11g express  详见: <Windows 7 x64 安装 Oracle 11g Express> 2. 安装 Micr ...

随机推荐

  1. hdoj--1068--Girls and Boys(最大独立集)

    Girls and Boys Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  2. [SPOJ 30669] Ada and Trip

    [题目链接] https://www.spoj.com/problems/ADATRIP/ [算法] 直接使用dijkstra堆优化算法即可 [代码] #include<bits/stdc++. ...

  3. [JavaEE] DWR入门教程

    DWR(Direct Web Remoting)是一个WEB远程调用框架.利用这个框架可以让AJAX开发变得很简单.利用DWR可以在客户端利用JavaScript直接调用服务端的Java方法并返回值给 ...

  4. docker(部署常见应用):docker部署nginx

    上回说到centos安装docker. 这次用实战,docker部署运行常见的应用. docker常用命令 参看:docker命令大全.这里不做赘述. docker部署nginx 1.docker h ...

  5. 28. Implement strStr()[E]实现strStr()

    题目 Implement strStr(). Return the index of the first occurrence of needle in haystack, or -1 if need ...

  6. Java实现一个简单的网络爬虫

    Java实现一个简单的网络爬虫 import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileWri ...

  7. C - Elephant(贪心)

    Problem description An elephant decided to visit his friend. It turned out that the elephant's house ...

  8. VS2005常用的快捷键分享

    VS2005代码编辑器的展开和折叠代码确实很方便和实用.以下是展开代码和折叠代码所用到的快捷键,很常用: Ctrl + M + O: 折叠所有方法 Ctrl + M + M: 折叠或者展开当前方法 C ...

  9. (转载)Activity-数据状态的保存

    由于手机是便捷式移动设备,掌握在用户的手中,它的展示方向我们是无法预知的,具有不确定性.平时我们拿着手机多数为竖屏,但有时候我们感觉累了也会躺着去使用手机,那么这时手机屏幕的展示方向可能已经被用户切换 ...

  10. Module, Package in Python

    1.To put it simple, Module是写好的一系列函数或变量,文件以.py为后缀,可以在其他Module中整体或部分引用. PS: 在Module中[结尾或开头]加入if __name ...