1

I have added the proxy to the package.json in my react app, I have the proxy server up and running on port 3001 - I can hit it with my browser.

I have tried using both axios and fetch, it doesn't seem to matter. Here's a link to the repo if you want to check it out.

Otherwise, here is the package.json in my react app:

{
  "name": "client",
  "version": "0.1.0",
  "private": true,
  "proxy": "http://localhost:3001/",
  "dependencies": {
    "axios": "^0.17.1",
    "react": "^16.0.0",
    "react-dom": "^16.0.0",
    "react-scripts": "1.0.14"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test --env=jsdom",
    "eject": "react-scripts eject"
  }
}

My super simple server...

const express = require("express");
const bodyParser = require("body-parser");
const app = express();
const PORT = process.env.PORT || 3001;
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use(express.static("client/build"));

app.get("/data", function(req, res){
  res.send({someData: 1234});
});

app.listen(PORT, function() {
   console.log("🌎  ==> API Server now listening on PORT ${PORT}!");
});

And inside my App.js, I try to call the API inside the componentDidMount function.

componentDidMount(){
    fetch("/data").then((data) => console.log(data));
    axios.get('/data').then(data => console.log(data)); 
}

Both of these API calls return a 404, and url of http://localhost:3000/data

Any ideas?

1 Answer 1

1

There's not really enough info here to properly diagnose this, but I'll take a stab at it. I'm going to have to make some assumptions, though.

First, the code repository you link to is not your complete project. My guess is that you're attempting to run this node.js server as an API server and you have something else serving up your web page and associated assets. Why do I think this? Because your code repository does not include anything that would server up the index.html file in the /client/public directory.

So here's what I think your environment looks like:

  • you have your node.js server running and listening on port 3001
  • you have your HTTP server running and listening on port 3000

And here's what's happening:

  1. You make a request to http://localhost:3000/ which gives you your index.html file and your Javascript, CSS, and other assets.

  2. Your React component makes a GET request to /data. Since you haven't specified the full URL, the browser has to guess what you mean. In this case, it will guess that you're accessing a path on the same host that it requested the page from, which is http://localhost:3000. So it makes a GET request to http://localhost:3000/data. Remember, your API is actually listening on http://localhost:3001/data.

  3. Your HTTP server doesn't have anything at that path, so it returns a 404 error

The solution? In this environment, don't use relative paths when you're making GET requests in your Javascript. Use the full URL.

Sign up to request clarification or add additional context in comments.

5 Comments

Regarding point 3, that's true, but that's what a proxy is supposed to do for me. I say axios.get("/data") and it will try to get it from localhost:3001/data If I try to use the full url as you suggest, react gives me an error "No 'Access-Control-Allow-Origin'" So I have to use a proxy. I have other apps doing exactly this... I'm just stumped as to why it isn't working this time around
Also correct that this is not the full app... this is something I wrote in an attempt to isolate the issue, nothing more
Post the proxy details and perhaps we can help with that.
It's in the client package.json above at line 4. That's all that has been necessary every other time I've done this. But I just followed a friend's suggestion to run the the two servers in separate terminals, and the proxy works. I had a script written to run them simultaneously using an npm package "concurrently." That always worked before, but whatever. I'm in no position to be picky, I need to get this project underway. Thanks for taking the time to reply
looks like it has all the details included in the question. the fetch location, the proxy location, and the express app. the only other piece of info i could think of including is the bash commands they used when they started both servers.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.