首页 > 解决方案 > 除了 Spotify 登录重定向路径“在服务器上找不到”之外,生产 React 应用程序正常运行

问题描述

生产 ReactJS 应用程序,使用 Google App Engine集成 Spotify Web Api ( https://developer.spotify.com/documentation/web-api/reference-beta/ ): https ://widget-dashboard-app.appspot.com/

所有路由 ( /settings, /about, /contact) 都按预期工作,但是当用户尝试登录 Spotify 时,重定向路径/settings/#access_token=[BIG-LONG-TOKEN]&token_type=Bearer&expires_in=3600会通过 GAE 导致此错误:

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

我的第一个想法是我的app.yaml文件中有问题:

# [START runtime]
runtime: python27
api_version: 1
threadsafe: true
# [END runtime]

# [START handlers]
handlers:
- url: /
  static_files: build/index.html
  upload: build/index.html
- url: /
  static_dir: build
- url: /.*
  static_files: build/static/\1
  upload: build/static/.*
- url: /settings/.*
  static_files: build/static/\1
  upload: build/static/.*
# [END handlers]

我已经搜索了两个星期的互联网搜索可能是什么问题。我都读过了。这。文档。

实际结果应该是:用户通过 Spotify 的 Web API 登录,并被重定向回https://widget-dashboard-app.appspot.com/settings/#access_token=[BIG-LONG-TOKEN]&token_type=Bearer&expires_in=3600,他们的浏览器将在几秒钟内开始播放音乐。这在开发中有效。

标签: reactjsgoogle-app-engineapp.yamlspotify-app

解决方案


由于它是一个 SPA,您的路由由应用程序处理,因此您需要一条包罗万象的路由,该路由位于您的 main index.html. 所以看起来你的通配符规则应该是这样的:

url: /.*
  static_files: build/index.html
  upload: build/index.html

/settings/我认为你不需要那个


推荐阅读