从浏览器访问前端SPA链接时,后端会触发找不到路由 [英] When accessing front-end SPA-links from browser, back-end fires finding no route

查看:75
本文介绍了从浏览器访问前端SPA链接时,后端会触发找不到路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用JS(ReactJS)编写的可以工作的前端单页应用程序,还有一个用Phoenix(Elixir)可以工作的后端.只要在应用程序中进行导航,一切都会正常进行.但是,当我尝试从浏览器访问SPA中的页面时,我收到了Phoenix发出的路由错误.例如:

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)

是从浏览器访问 http://localhost:4000/search 时得到的.

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

当我从SPA内的导航访问 http://localhost:4000/search 时,我得到了来自ReactJS的工作页面.

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

所以我的问题是:如何获得ReactJS而不是Phoenix来获取页面?

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

推荐答案

针对单页应用程序执行此操作的标准方法是,在每个GET路由上与在/上提供的服务完全相同.您可以将通配符GET路由添加到Phoenix路由器,并将其指向与/的路由相同的事物.如果/是通过PageControllerindex函数提供的,则应添加:

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

这篇关于从浏览器访问前端SPA链接时,后端会触发找不到路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆