带React的Rails部署到heroku后仅服务React页面,无法访问任何Rails路由

时间:2019-01-16 03:41:53

标签: ruby-on-rails ruby reactjs heroku

我正在使用一个现有的Rails应用程序,该应用程序已被重构为React和仪表板后端的api。 完整的React应用程序(带有React Router,Redux和Saga)已构建并安装到Rails根目录的client文件夹中。

在本地计算机上,我在端口3000上启动节点服务器,并在3001上启动Rails服务器。客户端文件夹中的package.json包括
"proxy": "http://localhost:3001/",

我可以访问React应用,但它无法从API获取数据。我可以在3001上访问Rails应用。日志错误是:

Proxy error: Could not proxy request /api/v1/homedata from localhost:3000 to http://localhost:3001/.

我的控制台显示此错误:

xhr.js:178 GET http://localhost:3000/api/v1/homedata 500 (Internal Server Error)

当我部署到Heroku时,React App可以在我的域中使用,但是我无法访问Rails仪表板。但是,React应用程序可以正确访问Rails API并显示数据。

我花了大约三天时间到处寻找一些想法。我目前的最佳猜测是Rails服务器或路由存在问题。但是我不知道在哪里看。我将发布路线。rb

    Rails.application.routes.draw do

  # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
root "dashboard/home#index"
  ### Routing for the dashboard ###
namespace :dashboard do
  resources :home, only: [:index]
  resources :connect_groups
  resources :events
  resources :event_types
  resources :locations
  resources :podcasts
  resources :users
  get "/ajax_load_events", to: "home#ajax_load_events"
  get "/ajax_load_locations", to: "home#ajax_load_locations"
  get "/ajax_load_podcasts", to: "home#ajax_load_podcasts"
  get "/ajax_load_connect_groups", to: "home#ajax_load_connect_groups"
  get "/ajax_load_users", to: "home#ajax_load_users"
  get '', to: 'home#index'
  post "dashboard/podcasts/upload", to: "audio_clips#create"
end

  devise_for :users, :controllers => {:registrations => "registrations"}

  ### Routing for the new API ###
  namespace :api, defaults: { format: 'json' } do
    namespace :v1 do
      resources :locations
      resources :podcasts
      resources :events
      resources :connect_groups
      resources :messages, only: [ :create]
      get "/location/*id", to: "locations#get_location"
      get "/event/featured", to: "events#featured_events"
      get "/homedata", to: "home#index"
    end
  end


  mount Shrine.presign_endpoint(:cache) => "/presign"

  match "/loaderio-0077704941f2bf1a629295ce9fb1a229", :to => proc { |env| [200, {}, ["loaderio-0077704941f2bf1a629295ce9fb1a229"]] }, via: :get

  get '*path', to: "application#fallback_index_html", constraints: ->(request) do
  !request.xhr? && request.format.html?
end

end

任何帮助或从哪里开始的任何想法都会带来巨大帮助。

1 个答案:

答案 0 :(得分:0)

解决了问题。这是不刷新浏览器缓存的问题。...