简体   繁体   English

动态将React App链接导航到Express服务器

[英]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.

相关问题 链接到 Express react 无需创建反应应用程序 - link to Express react WITHOUT creating-react-app 使用 express 服务器在 React 应用程序中热重载 - Hot reloading in a React app with express server 我的快速服务器的代理服务器在我的 React 应用程序中不起作用 - Proxy server to my express server is not working in my React app 从SQL Server保存/检索图像以将API表示为React App - Saving/Retrieving image from sql server to express api into react app 将快速服务器代码部署为反应应用程序的 Firebase 云功能 - Deploying express server code as firebase cloud function for react app 带有快速服务器的 React 应用程序仅显示空白页面 - React app with express server shows just blanc page 带有Express服务器的create-react-app无法在IE中呈现 - create-react-app with express server not rendering in IE 在 express 服务器上提供 create-react-app 构建文件 - Serving create-react-app build files on express server Express Server 无法从 React Native 应用程序获取请求 - Express Server cannot GET request from the React Native app 在 apache 服务器上部署 React (3000) 和 Express (8000) 应用程序 - Deploying React (3000) and Express (8000) app on the apache server
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM