NestJS enable cors in production

前端 未结 5 1244
萌比男神i
萌比男神i 2021-02-03 21:49

I\'ve enabled CORS in my NestJS app following the official tutorial, so my main.ts looks like the following:

import { FastifyAdapter, NestFactory }          


        
5条回答
  •  感情败类
    2021-02-03 22:08

    If you are running NestJs with graphql you will run into a problem where Apollo server will override the CORS setting see link. This below fixed the problem. I wasted 8 hrs of my life on this. :-( I hope you see this and you don't do that. see link and link

            GraphQLModule.forRoot({
                debug: process.env.NODE_ENV !== 'production',
                playground: process.env.NODE_ENV !== 'production',
                typePaths: ['./**/*.graphql'],
                installSubscriptionHandlers: true,
                context: ({req}) => {
                    return {req};
                },
                cors: {
                    credentials: true,
                    origin: true,
                },
            }),
    

    then in your main.ts:

            app.enableCors({
                origin: true,
                methods: 'GET,HEAD,PUT,PATCH,POST,DELETE,OPTIONS',
                credentials: true,
            });
    

提交回复
热议问题