When accessing front-end SPA-links from browser, back-end fires finding no route

∥☆過路亽.° 提交于 2020-02-05 07:40:12

问题


I have a working front-end single-page-application written in JS (ReactJS), and a working back-end in Phoenix (Elixir). Everything works out fine as long as navigation happens within the application. However, when I try to access a page in the SPA from the browser, I get a route error fired from Phoenix. For example:

no route found for GET /search (PhoenixApp.Router)

is what I get when I access http://localhost:4000/search from the browser.

When I access http://localhost:4000/search from the navigation inside the SPA, I get a working page from ReactJS.

So my question: How can I get ReactJS to get the page, rather than Phoenix?


回答1:


The standard way to do this for Single-Page Applications is to serve the exact same thing on every GET route as you're serving on /. You can add a wildcard GET route to your Phoenix Router and point it to the same thing as the route for /. If / is served from PageController's index function, you should add:

get "/*anything", PageController, :index


来源:https://stackoverflow.com/questions/41782764/when-accessing-front-end-spa-links-from-browser-back-end-fires-finding-no-route

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