Skip to content

Commit b2f63e5

Browse files
feat: config module
1 parent 9616157 commit b2f63e5

34 files changed

+465
-0
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,5 +58,11 @@
5858

5959
[jwt、session](./jwt-and-session)
6060

61+
[Node 里读取 env、yaml 配置文件](./config-test)
62+
63+
[Nest 的 Config 模块读取配置文件](./nest-config-test)
64+
6165
[typeorm + jwt + ValidationPipe 实现登录注册](./login-and-register)
6266

67+
68+

config-test/.env

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
aaa=1
2+
bbb=2

config-test/.production.env

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
aaa=111
2+
bbb=222

config-test/README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
npm install
2+
3+
node ./index.js
4+
5+
node ./index2.js
6+

config-test/hello.yaml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
application:
2+
host: 'localhost'
3+
port: 8080
4+
5+
db:
6+
mysql:
7+
url: 'localhost'
8+
port: 3306
9+
database: 'aaa'
10+
password: 'guang'

config-test/index.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
require('dotenv').config({
2+
path: process.env.NODE_ENVIRONMENT === 'production' ? '.production.env' : '.env',
3+
})
4+
5+
console.log('aaa', process.env.aaa);
6+
console.log('bbb', process.env.bbb)

config-test/index2.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
const yaml = require('js-yaml');
2+
const fs = require('fs');
3+
4+
const config = fs.readFileSync('./hello.yaml');
5+
6+
console.log(yaml.load(config));

config-test/package.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"name": "config-test",
3+
"version": "1.0.0",
4+
"description": "",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"keywords": [],
10+
"author": "",
11+
"license": "ISC",
12+
"dependencies": {
13+
"dotenv": "^16.3.0",
14+
"js-yaml": "^4.1.0"
15+
}
16+
}

nest-config-test/.aaa.env

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
aaa=3

nest-config-test/.env

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
aaa=1
2+
bbb=2

0 commit comments

Comments
 (0)