【发布时间】:2021-02-07 14:26:38
【问题描述】:
我正在使用此代码在 firebase 函数中运行我的后端
// Nest Dependencies
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { ExpressAdapter } from '@nestjs/platform-express';
// Firebase Functions Dependencies
import * as functions from 'firebase-functions';
import * as express from 'express';
// Create a express server
const server = express();
const cors = require('cors');
// Create a NestServer With the Express server
const createNestServer = async (expressInstance: any): Promise<void> => {
const app = await NestFactory.create(
AppModule,
new ExpressAdapter(expressInstance),
);
//Inititlize it
await app.init();
};
// Create the google cloud function with the Nest Server(express server)
export const v1 = functions.https.onRequest(async (request, response) => {
await createNestServer(server);
server.use(cors({origin:true}))
server(request, response);
});
但是我的 React 前端出现了这个 CORS 错误
跨域请求被阻止:同源策略不允许读取位于https://dev-api.mytingo.com/v1/user/teacher/groups/active?idTeacher=179 的远程资源。 (原因:缺少 CORS 标头“Access-Control-Allow-Origin”)。
我错过了什么? 感谢您的帮助
【问题讨论】:
标签: node.js google-cloud-functions cors nestjs