Skip to content

Commit d13f25a

Browse files
committed
Use dotenv to load env variables
1 parent 548fa27 commit d13f25a

File tree

5 files changed

+30
-4
lines changed

5 files changed

+30
-4
lines changed

.env.example

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
SERVER_PORT=8080
2+
MONGODB_HOST=mongodb://localhost:27017/dbname
3+
TOKEN_SECRET=secret
4+
TOKEN_EXPIRESIN=24h

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,6 @@
33

44
# compiled output
55
/dist
6+
7+
# config
8+
.env

package-lock.json

+14
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
"dependencies": {
1010
"bcryptjs": "^2.4.3",
1111
"cors": "^2.8.5",
12+
"dotenv": "^16.0.1",
1213
"express": "^4.17.3",
1314
"jsonwebtoken": "^8.5.1",
1415
"mongoose": "^6.2.9"

src/config/config.ts

+8-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1-
const SERVER_PORT = 8080;
2-
const MONGODB_HOST = 'mongodb://localhost:27017/notesappdb';
3-
const TOKEN_SECRET = 'secret';
4-
const TOKEN_EXPIRESIN = '24h';
1+
import dotenv from 'dotenv';
2+
3+
dotenv.config();
4+
5+
const SERVER_PORT = +process.env.SERVER_PORT!;
6+
const MONGODB_HOST = process.env.MONGODB_HOST!;
7+
const TOKEN_SECRET = process.env.TOKEN_SECRET!;
8+
const TOKEN_EXPIRESIN = process.env.TOKEN_EXPIRESIN!;
59

610
const config = {
711
server: {

0 commit comments

Comments
 (0)