[英]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. 我试图将React JS应用程序上的前端链接导航到Express服务器。
Example 例
<a href="/pages">Pages</a>
In the Express server, I wrote this piece of code 在Express服务器中,我编写了这段代码
app.get('/pages', (req, res) => {
res.send('Pages');
});
I have also updated my front end package.json file like so: 我还更新了前端package.json文件,如下所示:
"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. 我的React App在端口3000上运行,而Express服务器在端口5000上运行。但是,当我单击链接Pages时,没有任何反应。 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? 有什么方法可以使我的React App与Express服务器动态连接,而不必每次都在所有链接中指定
http://localhost:5000
?
Solved! 解决了! My package.json wasn't updated because of hot reloading.
由于热重装,我的package.json没有更新。 I had to kill the existing webpack server and run it again to make it work.
我不得不杀死现有的webpack服务器,然后再次运行它以使其正常运行。 In future, if anyone is stuck around with this situation, simply make sure you update your package.json file like so:
将来,如果有人遇到这种情况,只需确保像下面这样更新package.json文件:
"proxy": {
"/pathname" : {
"target" : "http://yourserveraddress"
}
}
I think it has to be 我认为一定是
"proxy": {
"/pages" : {
"target" : "http://localhost",
"port" : 5000
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.