Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Navigate a React App link to Express server dynamically

I am trying to navigate a front end link on React JS app to an Express server.

Example

<a href="/pages">Pages</a>

In the Express server, I wrote this piece of code

app.get('/pages', (req, res) => {
    res.send('Pages');
});

I have also updated my front end package.json file like so:

"proxy": {
   "/pages" : {
      "target" : "http://localhost:5000"
   }
}

My React App is running on port 3000 and Express server is running on port 5000. However, when I click on the link Pages nothing happens. I have to explicitly change the front end link like so to make it work:

<a href="http://localhost:5000/pages">Pages</a>

Is there any way how I could dynamically connect my React App with my Express server without having to specify http://localhost:5000 in all the links everytime?

like image 252
Singh Avatar asked Feb 25 '26 14:02

Singh


1 Answers

Solved! My package.json wasn't updated because of hot reloading. I had to kill the existing webpack server and run it again to make it work. In future, if anyone is stuck around with this situation, simply make sure you update your package.json file like so:

"proxy": {
   "/pathname" : {
       "target" : "http://yourserveraddress"
   }
 }
like image 191
Singh Avatar answered Feb 27 '26 04:02

Singh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!