I have used create-react-app to start a full stack React project. I am trying to make a request to my backend API which is running on port 3001 from the front end on port 3000. When I make the request I get an error in my terminal that says Proxy error: Could not proxy request /api/login from localhost:3000 to http://localhost:3001/
. I have found a number of Stack Overflow articles mentioning this issue and believe I have tried all the solutions to no avail.
From this post I tried adding the "secure" value to my proxy in the client side package.json
. I also tried adding a /
to the end of the proxy URL but that made no difference. The first snippet is my current set up and the second is what I had at first.
"proxy": {
"/api": {
"target": "https://localhost:3001",
"secure": false
}
},
"proxy": "http://localhost:3001"
From this post I tried adding --ignore client
to the start script in the server side package.json
. I also tried changing the proxy URL in the client side package.json
(seen above) from localhost:3001
to 127.0.0.1:3001
.
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node server.js",
"watch": "nodemon server.js --ignore client"
},
Finally, I read through this post and ensured that the backend route was set up correctly. I am able to get the console log I am looking for sometimes. What is strange is it sometimes works right after I start the server. Then after I refresh it breaks (returns 500) and never works again.
router.get('/login', async (req, res) => {
console.log('login works')
res.status(200).json({ message: req.session })
});
A few notes:
- I am using concurrently to start both backend and frontend at the same time.
- I have ensured no other terminals or containers are being run
- I am running Windows 10
- I have cors options set up to enable the
localhost:3000
origin
var corsOptions = {
origin: 'http://localhost:3000',
};
app.use(cors(corsOptions));