# nest-init **Repository Path**: zhang-jun27/nest-init ## Basic Information - **Project Name**: nest-init - **Description**: nest项目的初始化 - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2023-10-17 - **Last Updated**: 2023-10-17 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README 配置流程: 1.热重载配置 (1)pnpm i --save-dev webpack-node-externals run-script-webpack-plugin webpack (2)项目根目录新增一个 webpack-hmr.config.js 的配置文件,文件内容: const nodeExternals = require('webpack-node-externals'); const { RunScriptWebpackPlugin } = require('run-script-webpack-plugin'); module.exports = function (options, webpack) { return { ...options, entry: ['webpack/hot/poll?100', options.entry], externals: [ nodeExternals({ allowlist: ['webpack/hot/poll?100'], }), ], plugins: [ ...options.plugins, new webpack.HotModuleReplacementPlugin(), new webpack.WatchIgnorePlugin({ paths: [/\.js$/, /\.d\.ts$/], }), new RunScriptWebpackPlugin({ name: options.output.filename, autoRestart: false }), ], }; }; (3)在.eslintrc.js 文件下 新增规则 '@typescript-eslint/no-var-requires': 0, 解决require报错问题。 (4)pnpm i -D @types/webpack-env 解决第五步hot报错 (5)在main.ts 添加代码: if (module.hot) { module.hot.accept(); module.hot.dispose(() => app.close()); } (6)替换package.json中的start:dev命令为:"start:dev": "nest build --webpack --webpackPath webpack-hmr.config.js --watch" 2.配置Swagger文档 (1)pnpm install --save @nestjs/swagger (2)在main.ts 文件并使用 SwaggerModule 类初始化 Swagger: import { SwaggerModule, DocumentBuilder } from '@nestjs/swagger'; const config = new DocumentBuilder() .setTitle('标题') .setDescription('描述') .setVersion('1.0(版本号)') .build(); const document = SwaggerModule.createDocument(app, config); SwaggerModule.setup('api', app, document); (3)访问:路径+api 如:http://localhost:5000/api 3.数据库配置 (1)pnpm install --save @nestjs/typeorm typeorm mysql2 (2)在app.module.ts 中引入并使用: import { TypeOrmModule } from '@nestjs/typeorm'; imports:[ TypeOrmModule.forRoot({ type:'mysql', // 数据库类型 host:'localhost', // 数据库的连接地址host port:3306, // 数据库的端口 3306 username:'root', // 连接账号 password:'root', // 连接密码 database:'demo', // 连接的表名 retryDelay:500, // 重试连接数据库间隔 retryAttempts:10, // 允许重连次数 synchronize:true, // 是否将实体同步到数据库 autoLoadEntities:true, // 自动加载实体配置,forFeature()注册的每个实体都自己动加载 })]