【问题标题】:Unable to enable CORS in NestJS with app instance methods无法使用应用实例方法在 NestJS 中启用 CORS
【发布时间】:2019-03-06 21:26:08
【问题描述】:

我无法为 NestJs 启用 CORS 进行开发,使用 main.hmr.ts 中的大多数内置方法如下:

const app = await NestFactory.create(AppModule, { cors: true });

const app = await NestFactory.create(AppModule, { cors: { corsOptions... } });

app.enableCors() ;

app.enableCors({ corsOptions.. });

app.use(cors());

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-Origin', 'Content-Type, Accept');
} );

感觉就像我在 main.hmr.ts 中所做的一切都被忽略了。

唯一有效的是在控制器中使用 @Header 装饰器,即 @Header('Access-Control-Allow-Origin', '*')。

这在开发过程中作为临时措施是可以的,但长期来看并不理想。

我检查了编译后的 javascript,看起来还不错。我在 Windows 和 MacOS 上都试过了;同样的问题。我可能做错了什么?

【问题讨论】:

标签: typescript nestjs


【解决方案1】:

我正在更新 main.hmr.ts,它仅用于生产中的 webpack。我应该更新 main.ts。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-02-18
    • 2018-11-29
    • 2016-12-20
    • 1970-01-01
    • 2015-02-19
    • 2020-11-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多