Proxy in package.json is not working

Im trying to make a request to my local backend using a proxy, the same way I do when using create-react-app but it’s not working.

// package.json
...
proxy: "http://localhost:8000", // local django backend
...

The relay fetchQuery:

function fetchQuery(operation, variables) {
  return fetch("/graphql", {
    method: "POST",
    headers: {
      "Content-Type": "application/json"
    },
    body: JSON.stringify({
      query: operation.text,
      variables
    })
  }).then(response => {
    return response.json();
  });
}

Since I’m using the proxy, the request should go to http://localhost:8000/graphql

but the response is:

POST http://192.168.15.8:19006/graphql 404 (Not Found)

The problem is: it’s making a request to 192.168.15.8:19006, not using the proxy!

If I change the fetch to:

fetch("http://localhost:8000/graphql", ...

I get blocked by CORS policy.

Then, following how to fix local dev cors:

// webpack.config.js

const createExpoWebpackConfigAsync = require("@expo/webpack-config");

module.exports = async function(env, argv) {
  const config = await createExpoWebpackConfigAsync(env, argv);

 if (config.mode === "development") {
    config.devServer.proxy = {
      "/**": {
        target: {
          host: "localhost",
          protocol: "http:",
          port: 8000
        },
        secure: false,
        changeOrigin: true,
        logLevel: "info"
      }
    };

  return config;
};

Solves the problem. But would be great if the package.json proxy works, just like in create-react-app.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.