【问题标题】:Error when connecting nesjs app to SQL Server Express : failed to connect to localhost:1433 - self signed certificate将 nesjs 应用程序连接到 SQL Server Express 时出错:无法连接到 localhost:1433 - 自签名证书
【发布时间】:2021-08-01 12:31:11
【问题描述】:

我正在尝试将我的 nestjs 应用程序连接到 SQL Server Express,但出现此错误:

ConnectionError: 无法连接到 localhost:1433 - 自签名证书

注意:端口 1433 已打开,我可以通过 SSMS 进行连接:

import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { AppController } from './app.controller';
import { AppService } from './app.service';

@Module({
  imports: [
    TypeOrmModule.forRoot({
      type: 'mssql',
      host: 'localhost',
      port: 1433,
      username: 'sa',
      password: '<mypassword>',
      database: 'test',
      entities: [__dirname + '/**/*.entity{.ts,.js}']
    })
  ],
  controllers: [AppController],
  providers: [AppService],
})
export class AppModule {}

【问题讨论】:

    标签: nestjs sql-server-express


    【解决方案1】:

    使用这个 trustServerCertificate: true

    @Injectable()
    export class TypeOrmConfigService implements TypeOrmOptionsFactory {
      constructor(private configService: ConfigrationService) {}
    
      createTypeOrmOptions(connectionName?: string): TypeOrmModuleOptions | Promise<TypeOrmModuleOptions> {
        return Promise.resolve<TypeOrmModuleOptions>({
          type: 'mssql',
          name: connectionName,
          host: this.configService.dbHost,
          port: this.configService.dbPort,
          username: this.configService.dbUsername,
          password: this.configService.dbPassword,
          database: this.configService.dbName,
          entities: [__dirname + '/**/*.entity{.ts,.js}'],
          synchronize: true,
          autoLoadEntities: true,
          options: {
            encrypt: true,
          },
          extra: {
            validateConnection: false,
            trustServerCertificate: true,
          },
        });
      }
    }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-01-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-05
      • 2023-03-02
      • 1970-01-01
      相关资源
      最近更新 更多