NestJS enable cors in production

前端 未结 5 1240
萌比男神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:09

    Sad to know that you also tried:

    const app = await NestFactory.create(ApplicationModule);
    app.enableCors();
    await app.listen(3000);
    

    And it's still not working.


    Ensure that on your server side you have cors enabled, which should be something like this:

    app.use((req, res, next) => {
      res.header('Access-Control-Allow-Origin', '*');
      res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE');
      res.header('Access-Control-Allow-Headers', 'Content-Type, Accept');
      next();
    });
    

    And also ensure that your browser is cors supported. If all these still doesn't work, I will advice you download Allow-Control-Allow-Origin extension for Chrome, it should fix your issue.

提交回复
热议问题