简体   繁体   English

Sinatra和客户端路由

[英]Sinatra and a client-side routing

I have a simple application (server side is Sinatra, client side is ReactJS). 我有一个简单的应用程序(服务器端是Sinatra,客户端是ReactJS)。

The workflow is very basic: Sinatra handles get "/" request and sends an index.html to the client with static stylesheet and scripts. 工作流非常基础:Sinatra处理get "/"请求,并使用静态样式表和脚本将index.html发送到客户端。

Scripts are ReactJS app which consists of several components and a ReactRouter. 脚本是ReactJS应用程序,由几个组件和一个ReactRouter组成。 Each React's component is a distinct "page" with its own route/path in terms of the ReactRouter. 每个React组件都是一个不同的“页面”,就ReactRouter而言,它具有自己的路由/路径。

For example: 例如:

  • "/" => "index.html" (real html page with renered components inside) , "/" => "index.html" (real html page with renered components inside)
  • "/form" => (ReactRouter points to component <Form/>, in fact render happens inside selector of "index.html") , "/form" => (ReactRouter points to component <Form/>, in fact render happens inside selector of "index.html")
  • "/finish" => (ReactRouter points to component <Finish/>, in fact render happens inside selector of "index.html") . "/finish" => (ReactRouter points to component <Finish/>, in fact render happens inside selector of "index.html")

While I was implementing client side only, it worked pretty well. 当我仅实现客户端时,它运行良好。 But now, when I am trying to use Sinatra for the server side stuff this is broken: when I want to go to the /form I am getting Sinatra's default 404 page ("Sinatra doesn't know this ditty."). 但是现在,当我尝试将Sinatra用于服务器端时,这已经坏了:当我要转到/form我得到的是Sinatra的默认404页面(“ Sinatra不知道这个内容。”)。

I understand that Sinatra (or Rack) intercepts the request's path ( /form of /finish ) before the ReactRouter. 我了解Sinatra(或Rack)在ReactRouter之前拦截了请求的路径( /finish /form )。 But I cannot understand how to fix it (and why is it so, while router script is already on the client and should fire first). 但是我不知道如何解决它(为什么这样做,而路由器脚本已经在客户端上并且应该首先启动)。

Thanks for your help. 谢谢你的帮助。

Use wildcard route to return index.html to all route. 使用通配符路由将index.html返回到所有路由。

Something like this: 像这样:

get '/*' do
  #return index.html
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM