Node.js + Angular:Cors 问题 - fly.toml

问题描述 投票:0回答:0

我需要帮助解决“fly.io”平台上的部署问题。前端和后端之间的通信可以正常运行一段时间,但随后出现此错误:

“从来源‘https://todo-front.fly.dev’访问‘https://todo-back.fly.dev’的 XMLHttpRequest 已被 CORS 策略阻止:无‘Access-Control-Allow-Origin ' 标头存在于所请求的资源上。”

问题是我没有将标头从前端发送到后端,该标头之前正在发送但突然停止了。在我将它转移到这个平台之前,我的应用程序工作正常。请问别人是怎么解决这个问题的? 我的后端配置如下:

app.use(
  cors({
    origin: [
      "https://todo-front.fly.dev",
    ],
    credentials: true,
  })
);

后台日志: enter image description here

这是我的前端配置:

httpOptions = {
    headers: new HttpHeaders({
      'Content-Type': 'application/json',
      'Access-Control-Allow-Origin': '*',
    }),
    responseType: 'text' as 'json',
    withCredentials: true,
  };

fly.toml 前端:

app = "todo-front"
kill_signal = "SIGINT"
kill_timeout = 5
processes = []

[env]

[experimental]
  allowed_public_ports = []
  auto_rollback = true

[[services]]
  http_checks = []
  internal_port = 8080
  processes = ["app"]
  protocol = "tcp"
  script_checks = []
  [services.concurrency]
    hard_limit = 25
    soft_limit = 20
    type = "connections"

  [[services.ports]]
    force_https = true
    handlers = ["http"]
    port = 80

  [[services.ports]]
    handlers = ["tls", "http"]
    port = 443

  [[services.tcp_checks]]
    grace_period = "1s"
    interval = "15s"
    restart_limit = 0
    timeout = "2s"

fly.toml 后端:

app = "todo-back"
kill_signal = "SIGINT"
kill_timeout = 5
processes = []

[env]
  ALLOWED_ORIGINS = "https://todo-front.fly.dev"
[experimental]
  allowed_public_ports = []
  auto_rollback = true

[[services]]
  http_checks = []
  internal_port = 3000
  processes = ["app"]
  protocol = "tcp"
  script_checks = []
  [services.concurrency]
    hard_limit = 25
    soft_limit = 20
    type = "connections"

  [[services.ports]]
    force_https = true
    handlers = ["http"]
    port = 80

  [[services.ports]]
    handlers = ["tls", "http"]
    port = 443

  [[services.tcp_checks]]
    grace_period = "1s"
    interval = "15s"
    restart_limit = 0
    timeout = "2s"

提前谢谢你

我试图将 'Access-Control-Allow-Origin': 'https://todo-front.fly.dev' 更改为 'Access-Control-Allow-Origin': '*' 等没有任何作用。是托管的问题吗?

javascript node.js angular typescript
© www.soinside.com 2019 - 2024. All rights reserved.