question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Proxy setting not being applied to api calls

See original GitHub issue

Description

Adding "proxy": "url:port" to my package.json does not alter the port used in fetch requests.

Expected behavior

I have added "proxy": "http://localhost:8080" to my package.json within my root folder and so would expect that my fetch requests will be directed to this port.

Actual behavior

The requests are still going through to the port on which the create-react-app server is running, 3000 in this case. This has been observed using chrome network tools as well as using postman.

Environment

Run these commands in the project folder and fill in their results:

  1. react-scripts@0.8.3
  2. node: 7.2.0
  3. npm: 4.0.5
  4. yarn: 0.19.0-20161207.1240

Then, specify:

  1. Windows 10
  2. Chrome 55

Reproducible Demo

I’m not 100% sure on how to get a demo up and running, but here is the code that I am making my api call with and my package.json file for reference.

export function fetchEvents() {
  return dispatch => {
    fetch('/api/events')
      .then(res => res.json())
      .then(data => dispatch(setEvents(data.events)));
  }
}

package.json

{
  "name": "cra",
  "version": "0.1.0",
  "private": true,
  "devDependencies": {
    "flow-bin": "^0.36.0",
    "react-scripts": "0.8.3"
  },
  "dependencies": {
    "react": "^15.4.1",
    "react-dom": "^15.4.1",
    "react-redux": "^4.4.6",
    "react-router": "4.0.0-alpha.6",
    "redux": "^3.6.0",
    "redux-devtools-extension": "^1.0.0",
    "redux-thunk": "^2.1.0"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test --env=jsdom",
    "eject": "react-scripts eject"
  },
  "proxy": "http://localhost:8080"
}

Image of bug

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:2
  • Comments:21 (4 by maintainers)

github_iconTop GitHub Comments

16reactions
AdityaAnand1commented, Oct 20, 2017

I just ended up moving all my API endpoints to /api (and my static assets to /assets)

"proxy": {
    "/api": {
      "target": "http://localhost:3001"
    },
    "/assets": {
      "target": "http://localhost:3001"
    }
  },

Now my server only cares about the /api stuff and serving static files and react handles everything else.

9reactions
ProductOfAmericacommented, Jun 27, 2017

I’m having the same problem, but no matter what port I change it to, React still points api calls to http://localhost:3000/ instead of the proxy I provide.

package.json:

  "name": "client",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "react": "^15.6.1",
    "react-dom": "^15.6.1"
  },
  "devDependencies": {
    "react-scripts": "1.0.7"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test --env=jsdom",
    "eject": "react-scripts eject"
  },
  "proxy": "http://localhost:8080"
}

My Response:

Response {type: "basic", url: "http://localhost:3000/api/bots/", redirected: false, status: 401, ok: false…}
redirected:false
status:401
statusText:"Unauthorized"
type:"basic"
url:'htt:/localhost:3000/api/bots/'

My server is definitely running on localhost:8080, I can send requests to it. I think React is just ignoring my proxy? I’ve restarted both the server and React multiple times, changing ports multiple times as well. @gaearon

Read more comments on GitHub >

github_iconTop Results From Across the Web

React proxy not conecting to api url - Stack Overflow
try setting "proxy": "127.0.0.1:6000" in your package.json file for your client.
Read more >
Proxying API Requests in Development - Create React App
The development server will only attempt to send requests without text/html in its Accept header to the proxy.
Read more >
Proxy Settings in the API Definition - Tyk.io
Any requests coming into the host, on the port that Tyk is configured to run on, that go to this path will have...
Read more >
Set up a proxy integration with a proxy resource - Amazon API ...
The Lambda proxy integration, designated by AWS_PROXY in the API Gateway REST API, is for integrating a method request with a Lambda function...
Read more >
404 Unable to identify proxy for host: <virtual host name> and url
If the API proxy is not configured to accept the requests for the specific path used in the API Request URL, then we...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found