Angular 7 Routing in Google Cloud App Engine not working

谁说胖子不能爱 提交于 2020-07-06 09:44:28

问题


I've published an angular 7 Application to Google Cloud App Engine.

The index page is loading, but the subdirectorys give me

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

This is my 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

Edit: I finally figured out, how the routing in app.yaml works for Angular Applications. Here is my working 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/.*

回答1:


I think your routing rules in handlers work fine if your resource files are only js and css. If you have image files, audio files, etc, you must use a more generic routing rule with regex:

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

The idea is the same, but syntactically, a wild card match for any files with format filename.* will handle all the resource files.



来源:https://stackoverflow.com/questions/55863502/angular-7-routing-in-google-cloud-app-engine-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!