【问题标题】:Angular 7 Routing in Google Cloud App Engine not workingGoogle Cloud App Engine 中的 Angular 7 路由不起作用
【发布时间】:2019-09-15 17:24:28
【问题描述】:

我已经向 Google Cloud App Engine 发布了一个 Angular 7 应用程序。

索引页面正在加载,但子目录给了我

Error: Not Found
The requested URL /admin was not found on this server.

这是我的 app.yaml:

runtime: nodejs10


env_variables:
environment: "--prod"

handlers:

  - url: /
    static_files: dist/XXX/index.html
    upload: dist/XXX/index.html
  - url: /
    static_dir: dist/XXX/
  - url: /.*
    secure: always
    script: auto

编辑: 我终于想通了,app.yaml 中的路由如何适用于 Angular 应用程序。 这是我的工作 app.yaml:

runtime: nodejs10

env_variables:
  environment: "--prod"

handlers:

- url: /
  secure: always
  static_files: dist/index.html
  upload: dist/.*
- url: /(.*\.js)
  secure: always
  redirect_http_response_code: 301
  static_files: dist/\1
  upload: dist/.*\.js
- url: /(.*\.css)
  secure: always
  redirect_http_response_code: 301
  static_files: dist/\1
  mime_type: text/css
  upload: dist/.*\.css
- url: /.*
  secure: always
  static_files: dist/index.html
  upload: dist/.*

【问题讨论】:

  • 只有这个对我有用。
  • 本可以发布解决方案作为答案,但它似乎工作正常。

标签: angular google-app-engine google-cloud-platform google-cloud-sdk


【解决方案1】:

如果您的资源文件只有 js 和 css,我认为您在处理程序中的路由规则可以正常工作。如果您有图像文件、音频文件等,则必须使用更通用的路由规则和正则表达式:

handlers:
  - url: /
    secure: always
    static_files: www/index.html
    upload: www/index.html

  #  Routing rules for resources, css, js, images etc. Any file with format filename.ext
  - url: /(.*\.(.+))$
    secure: always
    static_files: www/\1
    upload: www/(.*\.(.+))$

  #  Routing rule for Angular Routing
  - url: /(.*)
    secure: always
    static_files: www/index.html
    upload: www/index.html

这个想法是一样的,但是在语法上,一个通配符匹配任何格式为 filename.* 的文件将处理所有的资源文件。

【讨论】:

  • 这是比问题 OP 中的自我回答更好的方法。
猜你喜欢
  • 2015-08-14
  • 2019-06-13
  • 2020-08-11
  • 2017-04-11
  • 1970-01-01
  • 2018-12-19
  • 1970-01-01
  • 1970-01-01
  • 2012-11-15
相关资源
最近更新 更多