正在获取JSON数据而不是前端代码



在我的express后端,这是我的路由器的顺序:
1.所有的api路由(例如:/notes(
2.发送回所有前端代码的catch-All路由。(react构建文件夹(

在客户端,我使用React Router,所有路由的名称都与api路由不同。(示例:/me/notes(
但是,现在当我转到route/notes(我的网站名称.com/notes(时,它会向我发回json数据,而不是客户端代码
无论如何,我怎么能总是发回前端代码而不是json数据?

在这种情况下,API和Frontend通常由2个不同的域名提供服务。

前端应用程序的主机应该在所有路由上返回index.html。

示例:

API: 
https://api.myapp.com/notes -> responds with notes list
Frontend: 
1. https://myapp.com -> returns index.html
2. https://myapp.com/<path> -> still returns index.html. 
React Router handles dynamic <path> when frontend app is loaded.

这实际上取决于宿主如何让服务器请求始终使用index.html进行响应,但我希望它能给你一个想法。

最新更新