Mixing server-rendering of pages with an API in Rails
This is the recommended way of serving API-endpoints in a regular Rails application. Note the {format:json} in the “api” section. Rails default is {format :html}
Also note that the same controller is serving both HTML and JSON, but the /api scope makes this easier to use.
Rails.application.routes.draw do
resources :products
scope "/api", defaults: {format: :json} do
resources :products
end
end