pnpm i @nestjs/config js-yaml cross-env -S
"start:dev":"cross-env NODE_ENV=development nest start --watch",
"start:prod": "cross-env NODE_ENV=production node dist/main",
"compilerOptions": {
"assets": [
"**/*.yml"
],
"watchAssets": true,
"deleteOutDir": true
}
src/config/index.ts
src/config/dev.yml
src/config/prod.yml
import { readFileSync } from 'fs';
import * as yaml from 'js-yaml';
import { join } from 'path';
const configFileNameObj = {
development: 'dev',
test: 'test',
production: 'prod',
};
const env = process.env.NODE_ENV;
console.log(env);
export default () => {
return yaml.load(readFileSync(join(__dirname, `./${configFileNameObj[env]}.yml`), 'utf8')) as Record<string, any>;
};
import { ConfigModule } from '@nestjs/config';
import configuration from './config/index';
// 配置模块
@Module({
imports: [UserModule,
ConfigModule.forRoot({
cache: true,
load: [configuration],
isGlobal: true,
}),],
controllers: [AppController],
providers: [AppService],
})
import { ConfigService } from '@nestjs/config';
const config = app.get(ConfigService);
pnpm i @nestjs/swagger -S
import { SwaggerModule, DocumentBuilder } from "@nestjs/swagger";
const swaggerOptions = new DocumentBuilder()
.setTitle("Nest Admin")
.setDescription("后台管理接口文档")
.setVersion("1.0")
.addBearerAuth()
.build();
const document = SwaggerModule.createDocument(app, swaggerOptions);
// 生产环境使用 nginx 可以将当前文档地址 屏蔽外部访问
SwaggerModule.setup(`${prefix}/swagger-ui`, app, document, {
swaggerOptions: {
persistAuthorization: true,
},
customSiteTitle: 'Nest-Admin API Docs',
});
import { VersioningType } from "@nestjs/common";
//URI: 0, URI 版本
// HEADER: 1, 标头版本
// MEDIA_TYPE: 2,媒体类型
// CUSTOM: 3 自定义版本
app.enableVersioning({
type: VersioningType.URI,
});
//Controller('v1') or {version: "1",}
pnpm i @nestjs/terminus -S
@Module({
imports: [
TerminusModule
],
})
pnpm i request-ip @types/request-ip -S
import { mw as requestIpMw } from 'request-ip';
app.use(requestIpMw({ attributeName: 'clientIp' }));
//Controller req.clientIp
console.log(req.clientIp)
pnpm i helmet -S
import helmet from 'helmet';
//web 安全,防常见漏洞 注意: 开发环境如果开启 nest static module 需要将 crossOriginResourcePolicy 设置为 false 否则 静态资源 跨域不可访问
app.use(helmet({ crossOriginOpenerPolicy: { policy: 'same-origin-allow-popups' }, crossOriginResourcePolicy: false }));
pnpm i express-rate-limit -S
import rateLimit from 'express-rate-limit';
// 设置访问频率
app.use(
rateLimit({
windowMs: 15 * 60 * 1000, // 15分钟
max: 1000, // 限制15分钟内最多只能访问1000次
}),
);
pnpm i --save @nestjs/typeorm typeorm mysql2
src/config/dev.yml
db:
mysql:
host: '127.0.0.1'
username: 'root'
password: '123456'
database: 'test'
port: 3306
charset: 'utf8mb4'
logger: 'file'
logging: true
multipleStatements: true
dropSchema: false
synchronize: false
supportBigNumbers: true
bigNumberStrings: true
src/app.module.ts
import { TypeOrmModule } from '@nestjs/typeorm';
import { ConfigModule, ConfigService } from '@nestjs/config';
@Module({
imports: [
TypeOrmModule.forRootAsync({
imports: [ConfigModule],
inject: [ConfigService],
useFactory: (configService: ConfigService) => {
return {
type: 'mysql',
...config.get('db.mysql'),
} as TypeOrmModuleOptions
}
})
]
})