使用快速路由显示 React 页面

Use express route to show React page

所以我正在尝试将我的 React 应用程序连接到快速路由 - 到目前为止,对于我的服务器,我有

const express = require('express');
const app = express();
const port = 5000;

app.use('/test', require('./client/src/App'));

app.listen(port, () => console.log(`Server started on port ${port}`));

我希望能够前往 localhost:5000/test 并显示我的 create-react-app (./client/src/App)。

我的服务器 运行 在 5000 上,React 运行 在 3000 上。

我知道 React 有自己的路由,但我想知道是否可以这样。

这样不行。 Express.js 不知道 react 这样的事情。您需要使用 react-router 处理 react 应用程序的路由,而不是这种方式。

Express.js 只负责在 express 而非 react.

的上下文中创建端点

如果您想使用这样的快捷方式,您可能只能使用 React 构建而不是 React 源。

否则,如果你想要 React 开发环境(比方说 运行 on localhost:3000),你应该可以在 express or nginx.[=12= 上使用反向代理]

Express 用于服务器端应用程序,React 用于客户端应用程序,两者是不同的应用程序,因此您无法使用 Express 渲染 React 页面,唯一的方法是 SSR(服务器端渲染),没有它是不可能的。