API subdomain for Heroku app, is it possible?

前端 未结 1 1082
闹比i
闹比i 2021-01-02 12:23

I am trying to build an API and I am concerned that all my resources will either not be accessible with the api.myapp.com domain or that they will \"live\" with the wrong ur

1条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-02 13:08

    If you want to differentiate between api.mydomain.com and www.mydomain.com and have different controllers for your API requests then you could certainly use Rails routes constrained to your api subdomain to handle this

    constraints :subdomain => "api" do
      scope :module => "api", :as => "api" do
       resources :posts
      end
    end
    

    which would then use the posts_controller.rb in the app/controllers/api folder of your application.

    You'll then have both www.mydomain.com and api.mydomain.com added a custom domains for your application and then the routes will take care of the rest.

    You might also want to look into the Grape Gem for helping build your api

    0 讨论(0)
提交回复
热议问题