标签: nestjs

Nestjs Swagger css 在部署到 vercel 时未加载

Nestjs swagger ui 在部署到 vercel 时不加载样式,但在本地运行良好

在此输入图像描述

控制台和网络请求 在此输入图像描述

在此输入图像描述

我使用以下配置添加了 vercel.json 并部署到 vercel。

{
  "version": 2,
  "builds": [
    {
      "src": "src/main.ts",
      "use": "@vercel/node"
    }
  ],
  "routes": [
    {
      "src": "/(.*)",
      "dest": "src/main.ts",
      "methods": ["GET", "POST", "PUT", "PATCH", "DELETE"]
    }
  ]
}
Run Code Online (Sandbox Code Playgroud)

主要.ts

const swaggerConfig = new DocumentBuilder()
  .setTitle('Tansfun')
  .setDescription('API for Tansfun')
  .setVersion('1.0')

  .addBearerAuth(
    {
      type: 'http',
      scheme: 'bearer',
      bearerFormat: 'APIKey',
      name: 'APIKey',
      description: 'Enter API Key',
      in: 'header',
    },
    'APIKey-auth', 
  )
  .build();

async function bootstrap() {
  const app = await NestFactory.create(AppModule);
  const …
Run Code Online (Sandbox Code Playgroud)

node.js swagger swagger-ui nestjs

1
推荐指数
1
解决办法
3184
查看次数

如何在 monorepo 中正确设置 typescript 以便编译私有包?

目前我已经设置了一个带有turborepo的monorepo,其中Nestjs作为BE,Nextjs作为FE。

我想重用 prisma 定义,所以很自然地将它分成自己的包并实现自己的 tsconfig。在我的数据库包的索引(棱镜所在的位置),我有这个简单的代码:

export * from "@prisma/client";

我的后端和前端现在都具有相同的依赖项: backend -> databasefrontend -> database

我的 FE 编译得很好,我可以使用我的 prisma 中的定义,但是 NestJS 应用程序没有在数据库包中编译 TS 并且有这个错误,我认为它与 tsconfig 有关,似乎 NestJS (我的后端)确实如此不想编译私有包依赖项,因此它无法识别“导出”。

core:dev: export * from "@prisma/client";
core:dev: ^^^^^^
core:dev: 
core:dev: SyntaxError: Unexpected token 'export'
Run Code Online (Sandbox Code Playgroud)

谁能指出我的回购协议有什么问题吗?

在导入 Nestjs 应用程序之前,我需要先构建数据库包吗?如果是这样,客户端为什么不先构建它就可以工作?

这是我的后端 tsconfig server/core/tsconfig.json

{
  "extends": "tsconfig/server.json",
  "compilerOptions": {
    "outDir": "./dist",
    "baseUrl": "./",
  },
}
Run Code Online (Sandbox Code Playgroud)

这是我的前端 tsconfig(工作正常)apps/web/tsconfig.json

{
  "extends": "tsconfig/nextjs.json",
  "include": ["next-env.d.ts", "**/*.ts", "**/*.tsx"],
  "exclude": ["node_modules"]
}
Run Code Online (Sandbox Code Playgroud)

和扩展,

tsconfig/server.json

{ …
Run Code Online (Sandbox Code Playgroud)

typescript reactjs next.js nestjs turborepo

1
推荐指数
1
解决办法
1588
查看次数

等待服务的异步初始化?

@Injectable()
export class MyService {
  ...
  constructor(@Inject('CONFIG') private config:ConfigOptions){
    this.connection= // await initConnection(config); <-- this returns a promise! How to await this?
  }
   
  send(payload:string){
    this.connection.send(payload)
  }
}
Run Code Online (Sandbox Code Playgroud)

如何await异步调用以initConnection()确保服务在使用前已初始化?

async-await typescript nestjs

1
推荐指数
1
解决办法
374
查看次数

NestJS:是否可以对不同的字段名称重复使用相同的 DTO 验证?

可以注意到,当您使用 DTO 而不是管道(取决于具体情况)时,验证主体甚至路由/查询参数可以更简单、更安全且更具可读性。作为一个简单的示例,我喜欢使用 DTO 来验证 id 的想法,就像在 findOne() 路由中一样。所以,有了这样的课程

export class IdDto {
  @IsInt()
  @IsPositive()
  id: number;
}
Run Code Online (Sandbox Code Playgroud)

我可以很好地验证(序列)id。并在路由处理程序中使用它,如下所示

findOne(@Param() { id }: IdDto)
Run Code Online (Sandbox Code Playgroud)

问题是当我有多个 id 作为路由参数时。显然我必须以不同的方式命名它们,然后我就不能再使用这个 DTO 了。除非我为像这样的每种情况创建一个特定的 DTO,只需重命名字段即可。在这种情况下,是否有任何方法可以维持此 DTO(或任何其他)的使用?或者真的没有其他选择吗?

当然,我可以建造自己的管道。只是 DTO 方法要简单得多,而且类验证器装饰器会自动显示验证失败的具体步骤。但也许这是除了多个类似的 DTO 之外的唯一选择。

预先致谢,并致以最诚挚的问候。

PS:在我提到的多个 id 的情况下,情况会稍微复杂一些。但如果我仍然只有一个 id,但想在路由中以不同的方式命名它?同样的问题也适用,以防它可能更简单。

class-validator nestjs

1
推荐指数
1
解决办法
77
查看次数

NestJs 不在 Docker 内部进行热重载工作

最小复制代码

示例代码

重现步骤
  1. npm 安装
  2. 码头工人组成
预期行为

等待docker容器创建并运行nest,修改main.ts文件,nest监听不执行热重加载

包.json
"dependencies": {
    "@nestjs/common": "^9.0.0",
    "@nestjs/core": "^9.0.0",
    "@nestjs/platform-express": "^9.0.0",
    "reflect-metadata": "^0.1.13",
    "rxjs": "^7.2.0"
  },
  "devDependencies": {
    "@nestjs/cli": "^9.0.0",
    "@nestjs/schematics": "^9.0.0",
    "@nestjs/testing": "^9.0.0",
    "@types/express": "^4.17.13",
    "@types/jest": "29.2.4",
    "@types/node": "18.11.18",
    "@types/supertest": "^2.0.11",
    "@typescript-eslint/eslint-plugin": "^5.0.0",
    "@typescript-eslint/parser": "^5.0.0",
    "eslint": "^8.0.1",
    "eslint-config-prettier": "^8.3.0",
    "eslint-plugin-prettier": "^4.0.0",
    "jest": "29.3.1",
    "prettier": "^2.3.2",
    "source-map-support": "^0.5.20",
    "supertest": "^6.1.3",
    "ts-jest": "29.0.3",
    "ts-loader": "^9.2.3",
    "ts-node": "^10.0.0",
    "tsconfig-paths": "4.1.1",
    "typescript": "^4.7.4"
  }
Run Code Online (Sandbox Code Playgroud)
Dockerfile
###################
# BUILD FOR LOCAL DEVELOPMENT
###################

FROM node:18-alpine As development

WORKDIR …
Run Code Online (Sandbox Code Playgroud)

node.js docker docker-compose nestjs

1
推荐指数
1
解决办法
2058
查看次数

运行 NestJS 微服务时找不到 this.assertNotInPreviewMode

我有一个使用 NestJS 的微服务设置。但是,我在运行时收到以下错误。

this.assertNotInPreviewMode('listen');
             ^

TypeError: this.assertNotInPreviewMode is not a function
Run Code Online (Sandbox Code Playgroud)

我已经使用安装了 NestJS 微服务包npm i --save @nestjs/microservices。不知道为什么该功能不存在。

node.js nest microservices nestjs

1
推荐指数
1
解决办法
471
查看次数

Nest JS - 无法向中间注入服务

我创建了一个auth middlewere来检查每个请求,中间是使用服务器(只有在req.connection中找不到数据).我正在尝试将服务注入到我的中间,我一直得到相同的错误"Nest无法解析AuthenticationMiddleware(?)的依赖关系.请验证当前上下文中是否有[0]参数可用."

AuthenticationModule:

@Module({
   imports: [ServerModule],
   controllers: [AuthenticationMiddleware],
})
export class AuthenticationModule {
}
Run Code Online (Sandbox Code Playgroud)

AuthenticationMiddleware:

@Middleware()
export class AuthenticationMiddleware implements NestMiddleware {

constructor(private service : UserService) {}

resolve(): (req, res, next) => void {
 return (req, res, next) => {
   if (req.connection.user)
    next();

  this.service.getUsersPermissions()     
  }
}
Run Code Online (Sandbox Code Playgroud)

ServerModule:

@Module({
 components: [ServerService],
 controllers: [ServerController],
 exports: [ServerService]
})    
 export class ServerModule {}
Run Code Online (Sandbox Code Playgroud)

ApplicationModule:

@Module({
  imports: [
    CompanyModule,
    ServerModule,
    AuthenticationModule
  ]
})

export class ApplicationModule implements NestModule{
  configure(consumer: MiddlewaresConsumer): void {
  consumer.apply(AuthenticationMiddleware).forRoutes(
      { path: …
Run Code Online (Sandbox Code Playgroud)

node.js nestjs

0
推荐指数
1
解决办法
1749
查看次数

使用MySQL docker容器网络设置的Node.js

我有一个运行在端口3000上的Node.js应用程序(使用新的NestJS)框架。作为数据库,我通过TypeORM使用MySQL。在本地,一切正常。我在将其码头化时遇到问题。

我的TypeORM配置:

{
    "type": "mysql",
    "host": "localhost",
    "port": 3306,
    "username": "root",
    "password": "root",
    "database": "nest",
    "entities": ["src/**/*.entity{.ts,.js}"],
    "synchronize": true
}
Run Code Online (Sandbox Code Playgroud)

docker-compose.yml的如下:

version: "3"
services:
  db:
    image: mysql
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: root
      MYSQL_DATABASE: nest
      MYSQL_USER: root
      MYSQL_PASSWORD: root
    networks:
      - new
  nest:
    image: grimscythe/nest-sample
    depends_on:
      - db
    ports:
      - 3000:3000
    networks:
      - new
networks:
  new:
Run Code Online (Sandbox Code Playgroud)

我一直在阅读有关此特定方案的文档,所有文档都应该可以正常工作。扑向MySQL容器表明数据库正在正常运行。但是,Node框架随处可见Unable to connect to the database...。我在docker-compose.yml文件中丢失了什么吗?

mysql node.js docker docker-compose nestjs

0
推荐指数
1
解决办法
740
查看次数

NestJS无法解析UsersModule的依赖项

NestJS无法解析UsersModule的依赖项。错误: Error: Nest can't resolve dependencies of the UsersModule (?). Please verify whether [0] argument is available in the current context.

app.module.ts:

@Module({
  imports: [
    ConfigModule,
    DatabaseModule,
    GraphQLModule,
    UsersModule,
  ],
  providers: [
    ErrorService,
  ],
  exports: [
    DatabaseModule,
    ErrorService,
  ],
})
export class AppModule implements NestModule {}
Run Code Online (Sandbox Code Playgroud)

users.module.ts:

@Module({
    imports: [
        DatabaseModule,
        ErrorService,
    ],
    providers: [
        UsersService,
        ...usersProviders,
        UserResolver,
    ],
})
export class UsersModule {
    constructor(private readonly errorService: ErrorService) {}
}
Run Code Online (Sandbox Code Playgroud)

问题是此ErrorService,但是例如数据库模块以类似的方式使用,并且可以正常工作而没有任何错误。我有点困惑)也许有人会帮忙。谢谢。

typescript nestjs

0
推荐指数
1
解决办法
1529
查看次数

nestjs Using ConfgService with TypeOrmModule

I set up a ConfigService as described in docs https://docs.nestjs.com/techniques/configuration

how can I use this service with the the TypeOrmModule?

TypeOrmModule.forRoot({
  type: 'mysql',
  host: 'localhost',
  port: 3306,
  username: 'root',
  password: 'root',
  database: 'test',
  entities: [__dirname + '/**/*.entity{.ts,.js}'],
  synchronize: true,
}),
Run Code Online (Sandbox Code Playgroud)

nestjs

0
推荐指数
2
解决办法
493
查看次数