Reputation: 1
I set up a minimal grape Api in my example rails application. I generated a Model (rails generate model Example (title:string, count:integer)), added to instances via the rails console and installed Grape 2.0.0.
I added api/root.rb in the app-directory:
class Root < Grape::API
format :json
get '/examples' do
Example.all
end
get '/examples/:id' do
Example.find(params[:id])
end
end
I added to config/routes.rb:
mount Root => "/api"
I modified config/application.rb and added:
config.autoload_paths << Rails.root.join('app', 'api')
# Load API endpoints
config.paths.add File.join('app', 'api'), glob: File.join('**', '*.rb')
config.autoload_paths += Dir[Rails.root.join('app', 'api', '*')]
Checking in the browser "localhost:3000/examples" return the expected JSON-Objects.
In a different directory (not the rails project) I set up a index.html file:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="./app.js" defer></script>
<title>Error Document</title>
</head>
<body>
</body>
</html>
and an app.js-file fetching from localhost:
fetch("http://localhost:3000/api/examples");
When I open index.html with LiveServer (running on 5501) the page reloads constantly and makes repeated GET-requests to the rails server. Sometimes it will eventually stop having loaded the data correctly, sometimes incorrectly. Anyone sees what is wrong here?
I tried to fetch from a different url (eg. 'https://dummyjson.com/products/1'), which worked absolutely fine without the repeated reloads. Hence I suspect the error to be within in rails app, being completely new to rails I could not pin point it though. Any help, hints or wake up calls are appreciated!
Upvotes: 0
Views: 30