node.js - 如何将 React 应用程序和 Nodejs 后端部署在子域的同一目录中?
问题描述
我有一个 React 应用程序,我用 Node js 制作了后端,也是 MongoDB 的服务器。它是一个 MERN 堆栈。我有一个目录结构,如:
-client // this is where react app, in build version is in client/build
-middleware
-models
-routes
package.json
server.js
...
我想在我的 Filezilla 中像这样部署它:
-test.Server22c
-backend // this is where all nodejs files
-static //these folder and other files are my build files in react app client/build
-index.html
...
我怎样才能安排在同一个文件夹中一起工作?我在我的 react redux 中更改了我的 Axios 帖子链接中的端点,但它不起作用
解决方案
最好的选择是使用2 个不同的端口,一个用于您的 react 应用程序,一个用于您的 node.js 服务器。
假设我们将使用:
- React App 的HTTP 默认端口80 ( http://example.com )
- Node.js 服务器的自定义端口8080 ( http://example.com:8080 )
反应
要部署 React,您可以简单地使用serve
,您可以在https://create-react-app.dev/docs/deployment/找到所需的一切。
你基本上需要在你的 react 目录中执行这些命令。
npm install -g serve
serve -s build -l 80
确保您的机器上没有运行任何 apache 服务器,否则端口 80 将被占用。
节点.js
你只需要在端口 8080 上运行你的服务器,我真的不知道你使用的是哪个框架,所以假设你使用的是 express,它在你的入口点看起来像这样index.js
。
app.listen(8080, function() {
console.log("Server is running on port 8080...");
});
推荐阅读
- python - 线程 'Thread-1' 中没有当前事件循环
- animation - Vue 3 项目中简单的路由器滑动过渡
- crystal-reports - 每当我拖动 Crystal Report Viewer 以显示表单时,它会下降而不从视图中显示
- arrays - 为什么每个值都没有初始化为 0?
- perl - 用于 DNA 序列反向互补的 Perl 代码
- asp.net-core - .Net Core 服务器端模型验证通过不显眼的 ajax 表单
- php - 如何使用foreach从html标签中获取动态值并将其保存到jquery变量以供momentObj
- java - 我可以将一些数组作为字段放在 java 类中并使用本机方法访问它吗?
- reactjs - 未处理的拒绝(TypeError):无法读取未定义的属性“signUp”
- django - Django电子邮件模板中的图像未加载到电子邮件中