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.

[Docs] Proxying API Requests in Development / Configuring the Proxy Manually - not working solution

See original GitHub issue

Link: https://create-react-app.dev/docs/proxying-api-requests-in-development#configuring-the-proxy-manually

The suggested code:

const proxy = require('http-proxy-middleware');
module.exports = function(app) {
  app.use(
    '/api',
    proxy({
      target: 'http://localhost:5000',
      changeOrigin: true,
    })
  );
};

doesn’t work. It fails with this error:

  proxy is not a function

Also, it’s not clear at all how to use this proxy.

How to enable one proxy for testing (local mock server) and another for development?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:2
  • Comments:10

github_iconTop GitHub Comments

1reaction
OnkelTemcommented, Mar 23, 2020

Let me share my solution.

I created src/setupProxy.js with the following content:

const { createProxyMiddleware } = require('http-proxy-middleware');
const packageJson = require('../package.json');

const args = process.argv.slice(2);
const mock = args[0] === '--mock';

module.exports = function(app) {
  app.use(
    '/api',
    createProxyMiddleware({
      target: mock ? packageJson.serverUrlMock : packageJson.serverUrlDev,
      changeOrigin: true,
    })
  );
};

and added these two extra lines in my package.json:

  "serverUrlMock": "http://localhost:3010",
  "serverUrlDev": "http://dev.server.domain:3010"

Now when I launch the app with the --mock parameter, it connects to the locally running mock server, and if leave it out, the client is connecting to our dev server.

This is an excerpt from the scripts section of my package.json:

  "scripts": {
    "start": "react-scripts start",
    "start-mock": "npx tsanalyzer-mock-server & react-scripts start --mock"
  }
0reactions
stale[bot]commented, Jun 5, 2020

This issue has been automatically closed because it has not had any recent activity. If you have a question or comment, please open a new issue.

Read more comments on GitHub >

github_iconTop Results From Across the Web

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 >
Configuring setupProxy.js using http-proxy-middleware
I have tried prepending my API URL within my fetch call with 'https://cors-anywhere.herokuapp.com/' ( https://github.com/Rob--W/cors-anywhere/ ) ...
Read more >
Configure a Proxy in React - James Q Quick
The Solution. To solve this problem, we can configure a proxy in the package.json file of the React project. This allows the app...
Read more >
Proxying API Requests in Development - Gatsby
This way, when you fetch('/api/todos') in development, the development server will recognize that it's not a static asset, and will proxy your request...
Read more >
API proxy configuration reference | Apigee X - Google Cloud
View Apigee Edge documentation. As a developer working with Apigee, your primary development activities involve configuring API proxies that function as ...
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