Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to correctly pass API requests to another backend server #248

Closed
ripitrust opened this issue Jul 28, 2016 · 6 comments
Closed

How to correctly pass API requests to another backend server #248

ripitrust opened this issue Jul 28, 2016 · 6 comments

Comments

@ripitrust
Copy link

Hi Team

  1. I have a backend python (flask) server running at localhost:5000, which provides API calls with json data
  2. Now I have the create-react-app running at localhost:3000, how should I pass the api calls back to localhost:3000?
  3. Previously, I use "proxy" option in the webpack-dev-server, but now I am not sure how to do it with create-react-app, do I absolutely have to eject it?

Thanks so much for the answer

@gaearon
Copy link
Contributor

gaearon commented Jul 28, 2016

We are discussing this in #147.
Can you show the proxy configuration you were using?
Also, is there a big reason why you can’t just embed http://localhost:5000 in your requests?

@ripitrust
Copy link
Author

ripitrust commented Jul 28, 2016

Hi @gaearon

Thanks so much for your reply

the devServer setting I used previously is

devServer: {
        contentBase: './js/dist',
        proxy: {
            "/api/*":{
                target:"http://localhost:5000/",
                secure:"false"
            },
            "/users" :{
               target:"http://localhost:5000/", 
               secure:"false" 
            }
        },
        hot:true,
        historyApiFallback: true,
        port:3000 // this is the webpack-dev-server port
  },

About your question
I am not sure if I am right , but if I want to fire a api request from a client served at localhost:3000 to a backend server listening at localhost:5000, I have to make the api request CORS compatible, right?
I think this will cause some problem

also, it means that in all my fetch(url) calls, I have to specifically put a complete url
fetch('http://localhost:5000/api').then()
instead of the api endpoint
fetch('/api').then()

@gaearon
Copy link
Contributor

gaearon commented Jul 28, 2016

👍 Thanks for explaining. I’ll close this in favor of #147 but I’ll make sure to revisit your use case when we work on this.

@gaearon gaearon closed this as completed Jul 28, 2016
@ripitrust
Copy link
Author

ripitrust commented Jul 28, 2016

Thanks Dan,
great, I will also look at #147 and see if it helps

@sergetoure09
Copy link

Hi @ripitrust
I think i have the same issue like you.I have my backend in python listening on port 8080 and i have used npm run build to create bundle.js.I use fetch to request json data from my backend API

componentDidMount(){
    var url="http://localhost:8080/"
    fetch(url).then(function(response){
        return response.json();
    }).then(function(data){
      this.update(data)
    });

and my update function is like this

 update(state){
    this.setState({
      first_name:state.last_name,
      last_name:state.last_name,
      position:state.position,
      avatar:{
        img:state.img,
        width:state.avatar.width,
        height:state.avatar.height},

      bio:state.bio,
      age:state.age
    })
  }

But i have a service workers NOT found issue when launhing my app.
Anyone can help?

nicosommi pushed a commit to nicosommi/create-react-app that referenced this issue Mar 17, 2018
* feat(react-scripts/prod): use new uglifyJsPlugin for ES6 support

this closes facebook#21, facebook#158, facebook#248

* feat(react-scripts/prod): execute uglify plugin parallel + enable caching
@janechou24
Copy link

Actually you could connect your react and flask in one step, just copy your react html file and bundle.js inside the public folder and put it inside the flask static folder, but pay attention about the relative path of the bundle.js, then if you run the flask again, you could see your react server. Hope that will help.

@lock lock bot locked and limited conversation to collaborators Jan 19, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants