I'm working on a Rails 5.0.6 as an API. I am also running a React application for the front end running on a node server version v9.8.0. On the React app which is running on localhost:3000
I get the following error:
The rails is used as an API so in the controllers I return @drinks
in json format.
drinks_controller.rb
class DrinksController < ApiController
# GET /drinks
def index
@drinks = Drink.select("id,title").all
render json: @drinks.to_json
end
# GET /drinks/1
def show
@drink = Drink.find(params[:id])
render json: @drink.to_json(:include => { :ingredients => { :only => [:id, :description] }})
end
end
I run both servers locally using Procfile
in Profile.dev
web: cd client && PORT=3000 npm start
api: PORT=3001 && bundle exec rails s
When I go to the rails server which is running on localhost:3001/api/drinks
I get the following:
[{"id":1,"title":"Sparkling Negroni"},{"id":2,"title":"Pineapple-Jalapeño Margarita"}]
which is in json format, On the app.js im fetching from that endpoint
class App extends Component {
componentDidMount() {
window.fetch('api/drinks')
.then(response => response.json())
.then(json => console.log(json))
.catch(error => console.log(error))
}
Is it the proxy not working?
{
"name": "package-json",
"version": "4.0.1",
"description": "Get metadata of a package from the npm registry",
"license": "MIT",
"repository": "sindresorhus/package-json",
"proxy": "http://localhost:3001",
"author": {
"name": "Sindre Sorhus",
"email": "sindresorhus@gmail.com",
"url": "sindresorhus.com"
}
I can't understand what the issue is?