Skip to content

Commit 63dfe64

Browse files
committed
env variables
1 parent 3194650 commit 63dfe64

File tree

3 files changed

+36
-37
lines changed

3 files changed

+36
-37
lines changed

session 5/chapters/1. Hello Nodejs World.md

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -66,40 +66,3 @@ Environment Variables are important to a software developer for multiple reasons
6666
With the increasing popularity of cloud computing, more applications are using cloud services and other external APIs. Most of these come with config keys for control and access management. If the API keys are added to the application, and the code is pushed to a public repository on GitHub, this could lead to an unmonitored access problem. Unknown parties might end up using your API keys, leading to an unintended bill for your cloud services, and other potential security issues.
6767

6868
To solve this problem, the config keys can be added as environment variables and invoked from a closed environment from where the application is deployed.
69-
70-
# Environment Variables in Node.js
71-
72-
In Node.js, process.env is a global variable that is injected during runtime. It is a view of the state of the system environment variables. When we set an environment variable, it is loaded into process.env during runtime and can later be accessed.
73-
74-
dotenv is a module available on npm to load environment variables into process.env. dotenv can be added to your Node.js project by installing it from npm or yarn:
75-
76-
```
77-
# with npm
78-
npm install dotenv
79-
80-
# or with Yarn
81-
yarn add dotenv
82-
```
83-
84-
Suppose we want to use sensitive credentials like username and password in an open-source project, we can use dotenv for that as well.
85-
86-
```javascript
87-
require('dotenv').config();
88-
89-
const mysql = require('mysql');
90-
let con = mysql.createConnection({
91-
host: process.env.DB_HOST,
92-
user: process.env.DB_USER,
93-
password: process.env.DB_PASS,
94-
});
95-
```
96-
97-
Now, to set the env variables, create a `.env` file at the root of the project directory.
98-
99-
```
100-
DB_HOST=localhost
101-
DB_USER=admin
102-
DB_PASS=password
103-
```
104-
105-
We can add this .env file to .gitignore so that our credentials are protected.
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Environment Variables in Node.js
2+
3+
In Node.js, process.env is a global variable that is injected during runtime. It is a view of the state of the system environment variables. When we set an environment variable, it is loaded into process.env during runtime and can later be accessed.
4+
5+
dotenv is a module available on npm to load environment variables into process.env. dotenv can be added to your Node.js project by installing it from npm or yarn:
6+
7+
```
8+
# with npm
9+
npm install dotenv
10+
11+
# or with Yarn
12+
yarn add dotenv
13+
```
14+
15+
Suppose we want to use sensitive credentials like username and password in an open-source project, we can use dotenv for that as well.
16+
17+
```javascript
18+
require('dotenv').config();
19+
20+
const mysql = require('mysql');
21+
let con = mysql.createConnection({
22+
host: process.env.DB_HOST,
23+
user: process.env.DB_USER,
24+
password: process.env.DB_PASS,
25+
});
26+
```
27+
28+
Now, to set the env variables, create a `.env` file at the root of the project directory.
29+
30+
```
31+
DB_HOST=localhost
32+
DB_USER=admin
33+
DB_PASS=password
34+
```
35+
36+
We can add this .env file to .gitignore so that our credentials are protected.
File renamed without changes.

0 commit comments

Comments
 (0)