1

Hey guys i was just working out on Proxy for calling nodejs server from react server, so after adding "proxy": "http://localhost:5000/"

to my package.json file in react i was able to redirect to localhost:5000 (which is running my node server) but it seems that Proxy only works with fetch and axios as

 axios.get("/api/currentuser")

redirects me to localhost:5000 but then when i try this on anchor tag like

<li><a href="/auth/google">Login with google</a></li>

it doesn't takes me to localhost:5000 so is there any way that sometimes working with tag if i need to stay at localhost:3000i should be there and whenever i need i can go to localhost:5000 ??? cause not for all anchor tag i need to redirect to localhost:5000

1 Answer 1

1

According to this post and react documentation you can use the file setupProxy.js to redirect requests based on certain criteria to a proxy and it also seems to work for href property.

You would have to adapt the example code:

const proxy = require('http-proxy-middleware');

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

to your requirements. You can use pattern matching on the api paths, so it should not be a problem. Hope this helps! :)

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

2 Comments

and what if i need to make this work on production level ??
Probably the best option is using a real proxy from which you serve the bundled code to redirect your requests just like you are doing in development mode

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.