CORS带有打字稿的无服务器框架?

发布于 2025-02-13 03:32:28 字数 6885 浏览 0 评论 0原文

我陷入了这个基本的CORS问题上,但似乎没有任何帮助。

在无服务器CORS生存指南中尝试了所有内容(

在这里,我获得了相同

浏览器中的错误 -

访问从'https://xxxxxxxxxx.execute-api.us-east-1.amazonaws.com/dev/space/spaces_config/ap1'来自origin'https://xxxxxxxxxxx'策略:请求标题字段访问权限控制 - 允许原素不允许访问控制者允许。前反应。

我的设置 - >

我正在使用此样板 - > https://github.com/sererver.com/sererverless/serverless/exampless/exampless/exampless/aws-node-node-node-node-node-node-amples-node-node-node-amples- REST-API-TYPESCRIPT

serverless.yml-

provider:
  name: aws
  runtime: nodejs16.x
  memorySize: 512
  timeout: 30
  environment:
    NODE_ENV: dev

plugins:
  - serverless-plugin-typescript
  - serverless-offline

package:
  exclude:
    - config/.env.stg
    - config/.env.pro
  include:
    - config/.env.dev

functions:
  createSpace:
    handler: handler.createSpace
    events:
      - http:
          path: spaces
          method: post
          cors:
            origin: "*"
            headers:
              - Content-Type
              - X-Amz-Date
              - Authorization
              - X-Api-Key
              - X-Amz-Security-Token
              - X-Amz-User-Agent
            allowCredentials: true
  findSpace:
    handler: handler.findSpace
    events:
      - http:
          path: spaces
          method: get
          cors:
            origin: "*"
            headers:
              - Content-Type
              - X-Amz-Date
              - Authorization
              - X-Api-Key
              - X-Amz-Security-Token
              - X-Amz-User-Agent
            allowCredentials: true

  findSpaceConfig:
    handler: handler.findSpaceConfig
    events:
      - http:
          path: spaces_config
          method: get
          cors:
            origin: "*"
            headers:
              - Content-Type
              - X-Amz-Date
              - Authorization
              - X-Api-Key
              - X-Amz-Security-Token
              - X-Amz-User-Agent
            allowCredentials: true
  updateSpaceConfig:
    handler: handler.updateSpaceConfig
    events:
      - http:
          path: spaces_config/{space_id}
          method: put
          cors:
            origin: "*"
            headers:
              - Content-Type
              - X-Amz-Date
              - Authorization
              - X-Api-Key
              - X-Amz-Security-Token
              - X-Amz-User-Agent
            allowCredentials: true
  findOneSpaceConfigById:
    handler: handler.findOneSpaceConfigById
    events:
      - http:
          path: spaces_config/{space_id}
          method: get
          cors:
            origin: "*"
            headers:
              - Content-Type
              - X-Amz-Date
              - Authorization
              - X-Api-Key
              - X-Amz-Security-Token
              - X-Amz-User-Agent
            allowCredentials: true

也尝试使用CORS:TRUE

Handler.js File-

import { Handler, Context, APIGatewayEvent } from "aws-lambda";
import dotenv from "dotenv";
// import middy from "middy";
// import { cors } from "middy/middlewares";

// cross-env NODE_ENV=dev ts-node handler.ts
dotenv.config({
  path: `config/.env.${process.env["NODE_ENV"]}`,
});

export * from "./app/connections/mongo";

import { SpacesController } from "./app/controller/spaces";
import { spaces } from "./app/model/spaces";

import { SpacesConfigController } from "./app/controller/spaces_config";
import { spaces_config } from "./app/model/spaces_config";
// Spaces

const spacesController = new SpacesController(spaces);

export const createSpace: Handler = (
  event: APIGatewayEvent,
  context: Context
) => {
  return spacesController.create(event, context);
};

export const findSpace: Handler = () => spacesController.find();

// Spaces Config

const spacesConfigController = new SpacesConfigController(spaces_config);

export const findSpaceConfig: Handler = () => spacesConfigController.find();

export const updateSpaceConfig: Handler = (event: APIGatewayEvent) =>
  spacesConfigController.update(event);

export const findOneSpaceConfigById: Handler = (
  event: APIGatewayEvent,
  context: Context
) => {
  return spacesConfigController.findOne(event, context);
};

// export const findOneSpaceConfigById = middy(findOneSpaceConfigByI).use(cors());

在样板中,有一个文件消息。 api typescript/app/utils/message.ts“ rel =” nofollow noreferrer”> https://github.com/serverless/examples/blob/master/aws-node-node-rest-api-typescript/app/message/message.ts.ts )我们可以自定义响应。 Middy不工作后,我尝试手动发送响应标题。

当我使用Middy时,我没有得到任何响应标头。

message.ts-

import { ResponseVO } from "../types/response";

enum StatusCode {
  success = 200,
}

class Result {
  private statusCode: number;
  private code: number;
  private message: string;
  private data?: any;

  constructor(statusCode: number, code: number, message: string, data?: any) {
    this.statusCode = statusCode;
    this.code = code;
    this.message = message;
    this.data = data;
  }

  /**
   * Serverless: According to the API Gateway specs, the body content must be stringified
   */
  bodyToString() {
    return {
      headers: {
        "Access-Control-Allow-Origin": "*",
        "Access-Control-Allow-Credentials": true,
        "Content-Type": "application/json",
        "Access-Control-Allow-Methods": "GET,PUT,POST,PATCH,OPTIONS",
        "Access-Control-Allow-Headers":
          "Content-Type, Accept, x-access-token ,Authorization",
      },
      statusCode: this.statusCode,
      body: JSON.stringify({
        code: this.code,
        message: this.message,
        data: this.data,
      }),
    };
  }
}

export class MessageUtil {
  static success(data: object): ResponseVO {
    const result = new Result(StatusCode.success, 0, "success", data);

    return result.bodyToString();
  }

  static error(code: number = 400, message: string) {
    const result = new Result(StatusCode.success, code, message);

    console.log(result.bodyToString());
    return result.bodyToString();
  }
}

依赖项 -

”依赖项

很长一段时间以来一直在问题上。

任何帮助将不胜感激。

谢谢

I am stuck on this basic CORS issue but nothing seems to help.

Tried everything in Serverless CORS Survival Guide(https://www.serverless.com/blog/cors-api-gateway-survival-guide/)

Everything working fine in serverless offline

Part 1 Serverless Offline

Here I am getting all the expected response headers for the same

Part 2 Serverless Offline

Error In Browser-

Access to fetch at 'https://XXXXXXXXX.execute-api.us-east-1.amazonaws.com/dev/spaces_config/ap1' from origin 'https://XXXXXXXXX.com' has been blocked by CORS policy: Request header field access-control-allow-origin is not allowed by Access-Control-Allow-Headers in preflight response.

My Setup->

I am using this boilerplate-> https://github.com/serverless/examples/aws-node-rest-api-typescript

Serverless.yml-

provider:
  name: aws
  runtime: nodejs16.x
  memorySize: 512
  timeout: 30
  environment:
    NODE_ENV: dev

plugins:
  - serverless-plugin-typescript
  - serverless-offline

package:
  exclude:
    - config/.env.stg
    - config/.env.pro
  include:
    - config/.env.dev

functions:
  createSpace:
    handler: handler.createSpace
    events:
      - http:
          path: spaces
          method: post
          cors:
            origin: "*"
            headers:
              - Content-Type
              - X-Amz-Date
              - Authorization
              - X-Api-Key
              - X-Amz-Security-Token
              - X-Amz-User-Agent
            allowCredentials: true
  findSpace:
    handler: handler.findSpace
    events:
      - http:
          path: spaces
          method: get
          cors:
            origin: "*"
            headers:
              - Content-Type
              - X-Amz-Date
              - Authorization
              - X-Api-Key
              - X-Amz-Security-Token
              - X-Amz-User-Agent
            allowCredentials: true

  findSpaceConfig:
    handler: handler.findSpaceConfig
    events:
      - http:
          path: spaces_config
          method: get
          cors:
            origin: "*"
            headers:
              - Content-Type
              - X-Amz-Date
              - Authorization
              - X-Api-Key
              - X-Amz-Security-Token
              - X-Amz-User-Agent
            allowCredentials: true
  updateSpaceConfig:
    handler: handler.updateSpaceConfig
    events:
      - http:
          path: spaces_config/{space_id}
          method: put
          cors:
            origin: "*"
            headers:
              - Content-Type
              - X-Amz-Date
              - Authorization
              - X-Api-Key
              - X-Amz-Security-Token
              - X-Amz-User-Agent
            allowCredentials: true
  findOneSpaceConfigById:
    handler: handler.findOneSpaceConfigById
    events:
      - http:
          path: spaces_config/{space_id}
          method: get
          cors:
            origin: "*"
            headers:
              - Content-Type
              - X-Amz-Date
              - Authorization
              - X-Api-Key
              - X-Amz-Security-Token
              - X-Amz-User-Agent
            allowCredentials: true

Also tried with cors: true

Handler.js File-

import { Handler, Context, APIGatewayEvent } from "aws-lambda";
import dotenv from "dotenv";
// import middy from "middy";
// import { cors } from "middy/middlewares";

// cross-env NODE_ENV=dev ts-node handler.ts
dotenv.config({
  path: `config/.env.${process.env["NODE_ENV"]}`,
});

export * from "./app/connections/mongo";

import { SpacesController } from "./app/controller/spaces";
import { spaces } from "./app/model/spaces";

import { SpacesConfigController } from "./app/controller/spaces_config";
import { spaces_config } from "./app/model/spaces_config";
// Spaces

const spacesController = new SpacesController(spaces);

export const createSpace: Handler = (
  event: APIGatewayEvent,
  context: Context
) => {
  return spacesController.create(event, context);
};

export const findSpace: Handler = () => spacesController.find();

// Spaces Config

const spacesConfigController = new SpacesConfigController(spaces_config);

export const findSpaceConfig: Handler = () => spacesConfigController.find();

export const updateSpaceConfig: Handler = (event: APIGatewayEvent) =>
  spacesConfigController.update(event);

export const findOneSpaceConfigById: Handler = (
  event: APIGatewayEvent,
  context: Context
) => {
  return spacesConfigController.findOne(event, context);
};

// export const findOneSpaceConfigById = middy(findOneSpaceConfigByI).use(cors());

In the boilerplate, there is a file message.ts(https://github.com/serverless/examples/blob/master/aws-node-rest-api-typescript/app/utils/message.ts) where we can customize the response.
After middy was not working above, I tried sending response headers manually.

I was not getting any response header when I was using middy.

message.ts-

import { ResponseVO } from "../types/response";

enum StatusCode {
  success = 200,
}

class Result {
  private statusCode: number;
  private code: number;
  private message: string;
  private data?: any;

  constructor(statusCode: number, code: number, message: string, data?: any) {
    this.statusCode = statusCode;
    this.code = code;
    this.message = message;
    this.data = data;
  }

  /**
   * Serverless: According to the API Gateway specs, the body content must be stringified
   */
  bodyToString() {
    return {
      headers: {
        "Access-Control-Allow-Origin": "*",
        "Access-Control-Allow-Credentials": true,
        "Content-Type": "application/json",
        "Access-Control-Allow-Methods": "GET,PUT,POST,PATCH,OPTIONS",
        "Access-Control-Allow-Headers":
          "Content-Type, Accept, x-access-token ,Authorization",
      },
      statusCode: this.statusCode,
      body: JSON.stringify({
        code: this.code,
        message: this.message,
        data: this.data,
      }),
    };
  }
}

export class MessageUtil {
  static success(data: object): ResponseVO {
    const result = new Result(StatusCode.success, 0, "success", data);

    return result.bodyToString();
  }

  static error(code: number = 400, message: string) {
    const result = new Result(StatusCode.success, code, message);

    console.log(result.bodyToString());
    return result.bodyToString();
  }
}

Dependencies Used-

Dependencies

Stuck on the problem for a very long time.

Any help will be appreciated.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

乖乖 2025-02-20 03:32:29

此问题很可能发生,因为浏览器的JavaScript正在发送无允许列表中未允许的列表中的标题。您可能不需要发送这些标头,因为从浏览器中发送它们不能帮助解决CORS并可能使其复杂化。我建议除了内容类型之外,从您的消息中删除所有标题,然后重试。

This issue is likely occurring because the JavaScript for the browser is sending headers that are not in the allowed list in your serverless.yml. You probably do not need to send those headers either as sending them from the browser doesn't assist in resolving CORS and may complicate it. I would recommend removing all headers from your message.ts except Content-type and trying again.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文