简体   繁体   English

Azure:连接/代理前端(React)到后端(Express/node.js)

[英]Azure: Connect/Proxy Frontend (React) to Backend (Express/node.js)

I am currently trying to connect a frontend (React) to a backend (Express/nodejs) within Azure App Services.我目前正在尝试在 Azure App Services 中将前端(React)连接到后端(Express/nodejs)。 I am using Windows, since "Virtual applications and directories" are currently not available for Linux.我正在使用 Windows,因为 Linux 目前无法使用“虚拟应用程序和目录”。 However, according to my research, that is necessary in this case.但是,根据我的研究,在这种情况下这是必要的。

Backend sample: server.js后端示例:server.js

const express = require('express');
const app = express();
const port = 3003;
require("dotenv").config(); // For process.env

[...]

app.get("/api/getBooks", async (req, res) => {
    const books = await Books.find();
    res.send(books);
});

Frontend sample: App.js前端示例:App.js

  const getBooks = () => {
    axios.get('/api/getBooks')
      .then(res => {
        setBooks(res.data);
        console.log("Got books: ")
        console.log(res.data);
      })
      .catch(err => {
        console.log(err);
      })
  }

Azure: Folder structure Azure:文件夹结构

site/server/server.js (Express)
site/wwwroot/index.html (React)

I successfully executed "npm install" via "Development Tools/Console".我通过“开发工具/控制台”成功执行了“npm install”。

The two are already connected via Virtual applications in Azure by using the following configuration.通过使用以下配置,两者已经通过 Azure 中的虚拟应用程序连接。

Virtual applications虚拟应用

The app generally loads succesfully.该应用程序通常会成功加载。 However, the connection to the backend is not working.但是,与后端的连接不起作用。

How can I start the node.js server now on Azure and make the proxy working?如何现在在 Azure 上启动 node.js 服务器并使代理正常工作?

I tried to start the server via "node server" on the console.我试图通过控制台上的“节点服务器”启动服务器。 But this does not seem to be working.但这似乎不起作用。

I discovered two possible ways to solve this issue.我发现了两种可能的方法来解决这个问题。

Assuming you have a client (client/App.js) and a server (server/server.js).假设您有一个客户端 (client/App.js) 和一个服务器 (server/server.js)。

Serve the React App via node.js/Express通过 node.js/Express 为 React App 提供服务

Based on the above architecture, a little bit of structure needs to be changed here.基于上面的架构,这里需要稍微改动一下结构。 Because the React app is no longer output through its own server, but directly through Express.因为 React app 不再是 output 通过自己的服务器,而是直接通过 Express。

In server/server.js , the following function must be called after express is declared.server/server.js中,声明 express 后必须调用以下 function。

app.use(express.static("../client/build"));

After defining some endpoints to the APIs, the last API node to define is the default route - the static output of the React build.在为 API 定义了一些端点之后,最后一个要定义的 API 节点是默认路由 - React 构建的 static output。

app.get("/", (res) => {
    res.sendFile(path.resolve(__dirname, "client", "build", "index.html"));
});

Using an FTP client, you can now create the /client/build directory that will contain the built React app.使用 FTP 客户端,您现在可以创建包含构建的 React 应用程序的/client/build目录。 Of course, another directory structure can be used.当然,可以使用另一种目录结构。

The client files from the built React app are then simply uploaded there.然后将构建的 React 应用程序中的客户端文件简单地上传到那里。

The deployment from the server is best done via Visual Studio Code and the Azure plugin.从服务器部署最好通过 Visual Studio Code 和 Azure 插件完成。

In the above structure, /server would then be deployed to your in the Azure extension (Azure/App Services --> Right click on "myapp" --> Deploy to Web App...)在上述结构中, /server然后将部署到您的 Azure 扩展(Azure/App Services --> 右键单击“myapp” --> 部署到 Web App...)

Create two App Services创建两个应用服务

For example: myapp.azurewebsites.net & myapp-api.azurewebsites.net例如:myapp.azurewebsites.net & myapp-api.azurewebsites.net

myapp must simply contain the built React app (/build) in the wwwroot directory. myapp必须在 wwwroot 目录中简单地包含已构建的 React 应用程序 (/build)。 This can be achieved via FTP.这可以通过 FTP 来实现。

The deployment from the /server to *myapp-api is best done via Visual Studio Code and the Azure plugin.从 /server 到 *myapp-api 的部署最好通过 Visual Studio Code 和 Azure 插件来完成。

In the above structure, /server would then be deployed to myapp-api in the Azure extension (Azure/App Services --> Right click on "myapp-api" --> Deploy to Web App...)在上述结构中,然后 /server 将部署到 Azure 扩展中的myapp-api (Azure/App Services --> 右键单击“myapp-api” --> 部署到 Web App...)

Also worth mentioning is that CORS should be configured, so that API calls can only be made from myapp.azurewebsites.net .另外值得一提的是,应该配置CORS ,这样 API 调用只能从myapp.azurewebsites.net 进行 This can be configured in the Azure Portal.这可以在 Azure 门户中进行配置。


Occasionally the node dependencies have to be installed afterwards via the SSH console in the Azure Portal.有时,之后必须通过 Azure 门户中的 SSH 控制台安装节点依赖项。 For me it sometimes worked automatically and sometimes not.对我来说,它有时会自动工作,有时不会。

To do this, simply change to the wwwroot directory (of the /server) and execute the following command.为此,只需切换到(/server 的) wwwroot目录并执行以下命令。

npm cache clean --force && npm install

Combine this with React Router将其与 React 路由器结合使用

React Router is usually used with React. React Router 通常与 React 一起使用。 This can be easily combined with a static-served web app from Express.这可以很容易地与 Express 的静态服务 web 应用程序结合使用。

https://create-react-app.dev/docs/deployment/#other-solutions https://create-react-app.dev/docs/deployment/#other-solutions

Excerpt摘抄

How to handle React Router with Node Express routing 如何使用 Node Express 路由处理 React Router

https://dev.to/nburgess/creating-a-react-app-with-react-router-and-an-express-backend-33l3 https://dev.to/nburgess/creating-a-react-app-with-react-router-and-an-express-backend-33l3

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 用于后端的节点JS Express和用于前端的React JS - Node JS Express for backend and React JS for frontend 如何在 node.js 后端使用 usestate 并响应前端 - how to use usestate with node.js backend and react frontend 我可以在同一个文件结构中同时运行我的 Node.js/Express 后端和 React.js 前端吗? - Can I run my Node.js/Express backend and React.js frontend together in the same file structure? 将Web令牌从后端传递到前端(Node.js + Express传递到AngularJS) - Passing web token from backend to frontend (Node.js + Express to AngularJS) 在Node.js/Express中等待后端数据库更新后如何更新前端? - How can I update the frontend after waiting for the database in the backend to update in Node.js/Express? nodejs:前端调用后端node.js程序时无法检索快速会话属性值 - nodejs: Fail to retrieve the express session attribute value when frontend calls backend node.js program 如何连接 Koa Js 后端和 React 前端 - How to connect the Koa Js backend with React Frontend Node.js Express和Nexmo,如何将备用路由连接到Vue.js前端 - Node.js Express and Nexmo, how to connect a backed route to the Vue.js frontend 来自前端的数据在后端重复两次 React.js Node.js - Data coming from frontend duplicates in backend twice React.js Node.js 分离的前端与bone.js和node.js后端令牌 - Separated frontend with backbone.js and node.js backend token
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM