nestjs / typeorm

TypeORM module for Nest framework (node.js) 🍇

Home Page:https://nestjs.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Conflict with Swagger Module causes cross-domain failure

pincman opened this issue · comments

I'm submitting a...


[ ] Regression 
[x] Bug report
[ ] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead post your question on Stack Overflow.

Current behavior

CORS Error after setup SwaggerModule and connection database by typeorm module
if not connect database,work fine with 200 status,or if not use swagger it work fine also,
but use them at same time will be error

Access to XMLHttpRequest at 'http://localhost:3000/api/posts' from origin 'http://localhost:3100' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

Expected behavior

when connection to database will work fine

Minimal reproduction of the problem with instructions

import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { RouterModule, Routes } from 'nest-router';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { UserModule } from './user/user.module';
const routes: Routes = [
  {
    path: '/user',
    module: UserModule,
  },
];

@Module({
  imports: [
    RouterModule.forRoutes(routes),
    UserModule,
    TypeOrmModule.forRoot({
      charset: 'utf8mb4',
      synchronize: false,
      logging: ['error'],
      type: 'mysql',
      host: '127.0.0.1',
      port: 3306,
      username: 'root',
      password: '123456',
      database: 'nestplus',
      subscribers: [],
      keepConnectionAlive: true,
      autoLoadEntities: true,
    }),
  ],
  controllers: [AppController],
  providers: [AppService],
})
export class AppModule {}

import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import {
  FastifyAdapter,
  NestFastifyApplication,
} from '@nestjs/platform-fastify';
import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger';
async function bootstrap() {
  const app = await NestFactory.create<NestFastifyApplication>(
    AppModule,
    new FastifyAdapter(),
    {
      logger: ['error', 'warn'],
      cors: true,
    },
  );

  const config = new DocumentBuilder()
    .setTitle('Cats example')
    .setDescription('The cats API description')
    .setVersion('1.0')
    .addTag('cats')
    .build();
  const document = SwaggerModule.createDocument(app, config);
  SwaggerModule.setup('api-docs', app, document);
  app.setGlobalPrefix('api');
  await app.listen(3000, '0.0.0.0');
}
bootstrap();

What is the motivation / use case for changing the behavior?

Environment

Nest version: 7.5.1

For Tooling issues:

  • Node version: v15.1.0
  • Platform: Mac

Others: