Skip to content

Commit b846e46

Browse files
author
zjb
committed
NODE_NEV 添加
1 parent 41295b1 commit b846e46

File tree

8 files changed

+182
-21
lines changed

8 files changed

+182
-21
lines changed

.env

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
DB=mysql
22
DB_HOST=127.0.0.1
3-
PORT=3000
3+
PORT=3000
4+
NODE_ENV="development"

README copy.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,8 @@ plainToInstance
2020
4. 二级路由
2121
5. icon
2222
6. 顶部工具栏
23+
24+
# npm-check
25+
26+
- npm install npm-check -g
27+
- npm-check -u

log/log.txt.1

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{"level":30,"time":1739159455235,"pid":7653,"hostname":"macdeMBP","msg":"UserController init"}
2+
{"level":30,"time":1739159457462,"pid":7653,"hostname":"macdeMBP","req":{"id":1,"method":"POST","url":"/auth/info","query":{},"params":{"0":"auth/info"},"headers":{"host":"localhost:3000","connection":"close","content-length":"0","sec-ch-ua-platform":"\"macOS\"","authorization":"Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6InRlc3QtMTIiLCJzdWIiOjE2LCJpYXQiOjE3MzkxNTg3MDksImV4cCI6MTc3MDI2MjcwOX0.mj9M0Mxv7iMR7PkfpGMzifgn7V6k2ms1g2fguFLEB7I","user-agent":"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/132.0.0.0 Safari/537.36","accept":"application/json, text/plain, */*","sec-ch-ua":"\"Not A(Brand\";v=\"8\", \"Chromium\";v=\"132\", \"Google Chrome\";v=\"132\"","sec-ch-ua-mobile":"?0","origin":"http://localhost:5173","sec-fetch-site":"same-origin","sec-fetch-mode":"cors","sec-fetch-dest":"empty","referer":"http://localhost:5173/","accept-encoding":"gzip, deflate, br, zstd","accept-language":"zh-CN,zh;q=0.9,en;q=0.8","cookie":"Hm_lvt_e8002ef3d9e0d8274b5b74cc4a027d08=1731487784,1731496783,1732862040,1733293609; Hm_lvt_b97569d26a525941d8d163729d284198=1731487784,1731496783,1732862040,1733293609"},"remoteAddress":"::1","remotePort":53979},"res":{"statusCode":201,"headers":{"x-powered-by":"Express","access-control-allow-origin":"*","content-type":"application/json; charset=utf-8","content-length":"305","etag":"W/\"131-gpHuL3QkYs1SFmyUWnpDUQ63yRk\""}},"responseTime":32,"msg":"request completed"}
3+
{"level":30,"time":1739159457687,"pid":7653,"hostname":"macdeMBP","req":{"id":2,"method":"GET","url":"/menu","query":{},"params":{"0":"menu"},"headers":{"host":"localhost:3000","connection":"close","sec-ch-ua-platform":"\"macOS\"","authorization":"Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6InRlc3QtMTIiLCJzdWIiOjE2LCJpYXQiOjE3MzkxNTg3MDksImV4cCI6MTc3MDI2MjcwOX0.mj9M0Mxv7iMR7PkfpGMzifgn7V6k2ms1g2fguFLEB7I","user-agent":"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/132.0.0.0 Safari/537.36","accept":"application/json, text/plain, */*","sec-ch-ua":"\"Not A(Brand\";v=\"8\", \"Chromium\";v=\"132\", \"Google Chrome\";v=\"132\"","sec-ch-ua-mobile":"?0","sec-fetch-site":"same-origin","sec-fetch-mode":"cors","sec-fetch-dest":"empty","referer":"http://localhost:5173/","accept-encoding":"gzip, deflate, br, zstd","accept-language":"zh-CN,zh;q=0.9,en;q=0.8","cookie":"Hm_lvt_e8002ef3d9e0d8274b5b74cc4a027d08=1731487784,1731496783,1732862040,1733293609; Hm_lvt_b97569d26a525941d8d163729d284198=1731487784,1731496783,1732862040,1733293609","if-none-match":"W/\"7c8-tA8VSiVHU1nhzqNq6JgV+74+bk4\""},"remoteAddress":"::1","remotePort":53980},"res":{"statusCode":304,"headers":{"x-powered-by":"Express","access-control-allow-origin":"*","etag":"W/\"7c8-tA8VSiVHU1nhzqNq6JgV+74+bk4\""}},"responseTime":5,"msg":"request completed"}
4+
{"level":30,"time":1739159457864,"pid":7653,"hostname":"macdeMBP","req":{"id":3,"method":"POST","url":"/auth/info","query":{},"params":{"0":"auth/info"},"headers":{"host":"localhost:3000","connection":"close","content-length":"0","sec-ch-ua-platform":"\"macOS\"","authorization":"Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6InRlc3QtMTIiLCJzdWIiOjE2LCJpYXQiOjE3MzkxNTg3MDksImV4cCI6MTc3MDI2MjcwOX0.mj9M0Mxv7iMR7PkfpGMzifgn7V6k2ms1g2fguFLEB7I","user-agent":"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/132.0.0.0 Safari/537.36","accept":"application/json, text/plain, */*","sec-ch-ua":"\"Not A(Brand\";v=\"8\", \"Chromium\";v=\"132\", \"Google Chrome\";v=\"132\"","sec-ch-ua-mobile":"?0","origin":"http://localhost:5173","sec-fetch-site":"same-origin","sec-fetch-mode":"cors","sec-fetch-dest":"empty","referer":"http://localhost:5173/","accept-encoding":"gzip, deflate, br, zstd","accept-language":"zh-CN,zh;q=0.9,en;q=0.8","cookie":"Hm_lvt_e8002ef3d9e0d8274b5b74cc4a027d08=1731487784,1731496783,1732862040,1733293609; Hm_lvt_b97569d26a525941d8d163729d284198=1731487784,1731496783,1732862040,1733293609"},"remoteAddress":"::1","remotePort":53981},"res":{"statusCode":201,"headers":{"x-powered-by":"Express","access-control-allow-origin":"*","content-type":"application/json; charset=utf-8","content-length":"305","etag":"W/\"131-gpHuL3QkYs1SFmyUWnpDUQ63yRk\""}},"responseTime":9,"msg":"request completed"}
5+
{"level":30,"time":1739159458040,"pid":7653,"hostname":"macdeMBP","req":{"id":4,"method":"GET","url":"/menu","query":{},"params":{"0":"menu"},"headers":{"host":"localhost:3000","connection":"close","sec-ch-ua-platform":"\"macOS\"","authorization":"Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6InRlc3QtMTIiLCJzdWIiOjE2LCJpYXQiOjE3MzkxNTg3MDksImV4cCI6MTc3MDI2MjcwOX0.mj9M0Mxv7iMR7PkfpGMzifgn7V6k2ms1g2fguFLEB7I","user-agent":"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/132.0.0.0 Safari/537.36","accept":"application/json, text/plain, */*","sec-ch-ua":"\"Not A(Brand\";v=\"8\", \"Chromium\";v=\"132\", \"Google Chrome\";v=\"132\"","sec-ch-ua-mobile":"?0","sec-fetch-site":"same-origin","sec-fetch-mode":"cors","sec-fetch-dest":"empty","referer":"http://localhost:5173/","accept-encoding":"gzip, deflate, br, zstd","accept-language":"zh-CN,zh;q=0.9,en;q=0.8","cookie":"Hm_lvt_e8002ef3d9e0d8274b5b74cc4a027d08=1731487784,1731496783,1732862040,1733293609; Hm_lvt_b97569d26a525941d8d163729d284198=1731487784,1731496783,1732862040,1733293609","if-none-match":"W/\"7c8-tA8VSiVHU1nhzqNq6JgV+74+bk4\""},"remoteAddress":"::1","remotePort":53982},"res":{"statusCode":304,"headers":{"x-powered-by":"Express","access-control-allow-origin":"*","etag":"W/\"7c8-tA8VSiVHU1nhzqNq6JgV+74+bk4\""}},"responseTime":4,"msg":"request completed"}
6+
{"level":30,"time":1739159459267,"pid":7653,"hostname":"macdeMBP","req":{"id":5,"method":"GET","url":"/user?pageNum=1&pageSize=10","query":{"pageNum":"1","pageSize":"10"},"params":{"0":"user"},"headers":{"host":"localhost:3000","connection":"close","sec-ch-ua-platform":"\"macOS\"","authorization":"Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6InRlc3QtMTIiLCJzdWIiOjE2LCJpYXQiOjE3MzkxNTg3MDksImV4cCI6MTc3MDI2MjcwOX0.mj9M0Mxv7iMR7PkfpGMzifgn7V6k2ms1g2fguFLEB7I","user-agent":"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/132.0.0.0 Safari/537.36","accept":"application/json, text/plain, */*","sec-ch-ua":"\"Not A(Brand\";v=\"8\", \"Chromium\";v=\"132\", \"Google Chrome\";v=\"132\"","sec-ch-ua-mobile":"?0","sec-fetch-site":"same-origin","sec-fetch-mode":"cors","sec-fetch-dest":"empty","referer":"http://localhost:5173/","accept-encoding":"gzip, deflate, br, zstd","accept-language":"zh-CN,zh;q=0.9,en;q=0.8","cookie":"Hm_lvt_e8002ef3d9e0d8274b5b74cc4a027d08=1731487784,1731496783,1732862040,1733293609; Hm_lvt_b97569d26a525941d8d163729d284198=1731487784,1731496783,1732862040,1733293609","if-none-match":"W/\"a72-OAg+MFOviiNvgnlJjyaB+xXppIM\""},"remoteAddress":"::1","remotePort":53995},"msg":"getUserList 请求成功"}
7+
{"level":30,"time":1739159459272,"pid":7653,"hostname":"macdeMBP","req":{"id":5,"method":"GET","url":"/user?pageNum=1&pageSize=10","query":{"pageNum":"1","pageSize":"10"},"params":{"0":"user"},"headers":{"host":"localhost:3000","connection":"close","sec-ch-ua-platform":"\"macOS\"","authorization":"Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6InRlc3QtMTIiLCJzdWIiOjE2LCJpYXQiOjE3MzkxNTg3MDksImV4cCI6MTc3MDI2MjcwOX0.mj9M0Mxv7iMR7PkfpGMzifgn7V6k2ms1g2fguFLEB7I","user-agent":"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/132.0.0.0 Safari/537.36","accept":"application/json, text/plain, */*","sec-ch-ua":"\"Not A(Brand\";v=\"8\", \"Chromium\";v=\"132\", \"Google Chrome\";v=\"132\"","sec-ch-ua-mobile":"?0","sec-fetch-site":"same-origin","sec-fetch-mode":"cors","sec-fetch-dest":"empty","referer":"http://localhost:5173/","accept-encoding":"gzip, deflate, br, zstd","accept-language":"zh-CN,zh;q=0.9,en;q=0.8","cookie":"Hm_lvt_e8002ef3d9e0d8274b5b74cc4a027d08=1731487784,1731496783,1732862040,1733293609; Hm_lvt_b97569d26a525941d8d163729d284198=1731487784,1731496783,1732862040,1733293609","if-none-match":"W/\"a72-OAg+MFOviiNvgnlJjyaB+xXppIM\""},"remoteAddress":"::1","remotePort":53995},"res":{"statusCode":304,"headers":{"x-powered-by":"Express","access-control-allow-origin":"*","etag":"W/\"a72-OAg+MFOviiNvgnlJjyaB+xXppIM\""}},"responseTime":6,"msg":"request completed"}
8+
{"level":30,"time":1739159466029,"pid":7688,"hostname":"macdeMBP","msg":"UserController init"}
9+
{"level":30,"time":1739159495956,"pid":7715,"hostname":"macdeMBP","msg":"UserController init"}
10+
{"level":30,"time":1739159508166,"pid":7735,"hostname":"macdeMBP","msg":"UserController init"}

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@
3939
"nestjs-pino": "^4.3.0",
4040
"passport": "^0.7.0",
4141
"passport-jwt": "^4.0.1",
42+
"pino-pretty": "^13.0.0",
43+
"pino-roll": "^3.0.0",
4244
"reflect-metadata": "^0.2.0",
4345
"rxjs": "^7.8.1",
4446
"typeorm": "^0.3.20"

pnpm-lock.yaml

Lines changed: 85 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/app.module.ts

Lines changed: 43 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,63 @@
1-
import { Module } from '@nestjs/common';
2-
import { AppService } from './app.service';
3-
import { UserModule } from './user/user.module';
4-
import { TypeOrmModule } from '@nestjs/typeorm';
5-
import { AuthModule } from './auth/auth.module';
6-
import { AppController } from './app.controller';
7-
import { RoleModule } from './role/role.module';
8-
import { MenuModule } from './menu/menu.module';
9-
import { RoleAccessModule } from './role_access/role_access.module';
10-
import { ConfigModule } from '@nestjs/config';
1+
import { Module } from "@nestjs/common";
2+
import { AppService } from "./app.service";
3+
import { UserModule } from "./user/user.module";
4+
import { TypeOrmModule } from "@nestjs/typeorm";
5+
import { AuthModule } from "./auth/auth.module";
6+
import { AppController } from "./app.controller";
7+
import { RoleModule } from "./role/role.module";
8+
import { MenuModule } from "./menu/menu.module";
9+
import { RoleAccessModule } from "./role_access/role_access.module";
10+
import { ConfigModule } from "@nestjs/config";
11+
import { LoggerModule } from "nestjs-pino";
12+
import { join } from "path";
13+
1114
// 模块注册中心
1215
@Module({
1316
//1. 定义数据库的连接
1417
imports: [
1518
ConfigModule.forRoot({
1619
isGlobal: true,
17-
envFilePath: '.env',
20+
envFilePath: ".env",
1821
}),
1922
TypeOrmModule.forRoot({
20-
type: 'mysql',
21-
host: 'localhost', // 127.0.0.1
23+
type: "mysql",
24+
host: "localhost", // 127.0.0.1
2225
port: 3306,
23-
username: 'root',
24-
password: '123456',
25-
database: 'admin',
26-
entities: [__dirname + '/**/*.entity{.ts,.js}'],
26+
username: "root",
27+
password: "123456",
28+
database: "admin",
29+
entities: [__dirname + "/**/*.entity{.ts,.js}"],
2730
synchronize: true,
2831
autoLoadEntities: true,
29-
connectorPackage: 'mysql2', // 指定使用 mysql2
32+
connectorPackage: "mysql2", // 指定使用 mysql2
3033
}),
3134
AuthModule,
3235
UserModule,
3336
RoleModule,
3437
MenuModule,
3538
RoleAccessModule,
39+
LoggerModule.forRoot({
40+
// 开发环境log 形式,线上文件形式,方便问题追踪
41+
pinoHttp: {
42+
transport:
43+
process.env.NODE_NEV === "development"
44+
? {
45+
target: "pino-pretty",
46+
options: {
47+
colorize: true,
48+
},
49+
}
50+
: {
51+
target: "pino-roll",
52+
options: {
53+
file: join("log", "log.txt"),
54+
frequency: "daily",
55+
size: "10m",
56+
mkdir: true,
57+
},
58+
},
59+
},
60+
}),
3661
],
3762
controllers: [AppController],
3863
providers: [AppService],

src/main.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,10 @@ async function bootstrap() {
4444
);
4545
// 使用 ConfigService 获取配置项
4646
const port = configService.get<number>("PORT"); // 通过环境变量读取端口
47+
const env = configService.get<number>("NODE_ENV"); // 通过环境变量读取环境
4748

4849
await app.listen(port);
49-
logger.log(`App 运行在 http://localhost:${port}`);
50+
logger.log(`App 运行在 http://localhost:${port} ${env}`);
5051
}
5152
bootstrap();
5253
/**

0 commit comments

Comments
 (0)