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. luogu2518 [HAOI2010] 计数

    题目大意 给出一个数字$n$,求满足下列条件的数$x$的个数: $x<n$ 对于来自于$x$十进制各个数位上的非零数字,它们的种类与个数都与$n$的相同. 思路 入手点 设$n$有$t$位数字, ...

  2. luogu4180 次小生成树Tree 树上倍增

    题目:求一个无向图的严格次小生成树(即次小生成树的边权和严格小于最小生成树的边权和) 首先求出图中的最小生成树.任意加一条树外边都会导致环的出现.我们现在目标是在树外边集合B中,找到边b∈B,a∈b所 ...

  3. Swift3.0中关于日期类的使用指引

    日期的处理在大大小小的iOS项目中都十分常见,随着Swift3.0正式版的即将推出,语法的改变让NSDate以及相关类的使用都与之前略有不同,这里将会对基于Swift3.0版本的NSDate及相关类的 ...

  4. asp.net的TextBox回车触发指定的按钮事件

    一;             event.returnValue = false;             document.all[button].click();         }    }   ...

  5. 多个tomcat配置,解决冲突问题

    一.一般修改 路径: /opt/apache-tomcat/conf/server.xml 1.第一个tomcat使用默认配置 2.第二个tomcat一般配置 二.特殊修改 1.第二个tomcat特殊 ...

  6. javascript 将单词首字母大写,其余小写

    // 1 别人写的,我拿来参考了一下 function titleCase(str) { var array = str.toLowerCase().split(" "); for ...

  7. wppay免登录付费查看隐藏内容/付费资源下载

    WPPAY是一款模板兔开发的免登录的付费查看内容/付费下载资源WordPress插件,WPPAY不需要用户注册登录即可支付查看隐藏内容,把整个流程做到极简.发布文章时要隐藏的内容可以利用短代码: [w ...

  8. MBR分区表格式 - 简明概述

    目前硬盘主要有MBR和GPT分区两种格式,前者是Windows XP之前时代主流的分区格式,后者则是现在Windows 8之后主流的分区格式.(Windows 7需要通过一些手段能实现支持GPT,而W ...

  9. Vue的前端路由

    vue-router-- 根据不同的地址找到不同的页面                                       (单页面应用:无需频繁的从后台刷新页面) 1,安装路由-->导 ...

  10. 有关Gradle Network is unreachable: connect的报错

    项目Gradle   Errer:Network is unreachable: connect 同时还有as的 报错 Internal HTTP server disabled: Cannot st ...