1 Star 0 Fork 0

Catlair / graduation-object-server

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

Nest Logo

用于构建高效且可扩展的服务器端应用程序的渐进式 Node.js 框架,深受 Angular 的启发。

NPM Version Package License NPM Downloads CircleCI Coverage Discord Backers on Open Collective Sponsors on Open Collective Support us

Description

Nest framework TypeScript starter repository.

安装依赖

包管理器使用 pnpm,可以使用 npmyarn 安装依赖。

$ pnpm install

运行应用

# development 开发环境
$ pnpm run start

# watch mode 监听文件变化
$ pnpm run start:dev

# production mode 生产环境
$ pnpm run start:prod

做了什么

开发环境

使用 cross-env NODE_ENV=development 为不同环境设置变量。

可用于加载不同的配置文件。

{ "start:dev": "cross-env NODE_ENV=development nest start --watch" }

配置文件

src/config/configuration.ts 文件中进行配置。
根据不同环境加载 .env 文件在 src/app.module.ts 修改(可能更喜欢单文件配置)。

在项目中使用 ConfigService 来获取配置。

// 注入
@Injectable()
export class HashingService {
  constructor(private readonly configService: ConfigService<Configuration>) {}

  async hash(value: string): Promise<string> {
    return await bcrypt.hash(value, this.configService.get('saltRounds'));
  }
}

// 在 main 中使用
const configService = app.get(ConfigService);
const PORT = configService.get<number>('port');

缓存

默认使用 redis 缓存,设置 REDIS_DISABLE=true 使用 memory

src/modules/auth/auth.module.ts src/modules/auth/auth.service.ts 中使用 CacheService 来获取缓存。

安全

接口限速

同上,使用的 redis 和内存缓存。

@Module({
  imports: [
    ThrottlerModule.forRootAsync({
      inject: [ConfigService],
      useFactory: async (conifg: ConfigService<Configuration>) => ({
        ttl: 60,
        limit: conifg.get('rateLimitMax'),
        storage: conifg.get('REDIS_DISABLE')
          ? null
          : new ThrottlerStorageRedisService(),
      }),
    }),
    AuthModule,
  ],
  providers: [
    {
      /** 节流器 */
      provide: APP_GUARD,
      useClass: ThrottlerGuard,
    },
  ],
})
export class AppModule {}

JWT

src/modules/auth
参考 src/decorators/auth.decorator.ts 中的装饰器进行鉴权。根据实际修改。
方法装饰器 @Auth('admin') 表示需要 admin 权限。
参数装饰器 @UserReq() user: User 获取已登录用户信息(会访问数据库)。 同上 @UserJwt() user: JwtDto 获取 JWT 用户信息(不会访问数据库)。 方法装饰器 AuthUnlogin() 表示可以不需要登录,但会尝试获取用户登录信息。

日志

日志使用 winston 来记录。 见 src/configsrc/middleware/logger.middleware.ts

Support

Nest is an MIT-licensed open source project. It can grow thanks to the sponsors and support by the amazing backers. If you'd like to join them, please read more here.

Stay in touch

License

Nest is MIT licensed.

MIT License Copyright (c) 2022 catlair Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

暂无描述 展开 收起
TypeScript 等 3 种语言
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
1
https://gitee.com/catlair/graduation-object-server.git
git@gitee.com:catlair/graduation-object-server.git
catlair
graduation-object-server
graduation-object-server
main

搜索帮助