无法准备我的 MERN 应用程序以进行部署 [英] Not able to prepare my MERN app for deployment

查看:13
本文介绍了无法准备我的 MERN 应用程序以进行部署的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我之前从未将 MERN 应用部署到生产环境中.这将是我的第一次尝试,我计划将该应用程序部署到数字海洋.

I have never deployed a MERN app to production before. This is going to be my first attempt and I am planning to deploy the app to digital ocean.

因此,我已按照 Udemy 课程中的说明准备好要部署的 MERN 应用程序.我的应用程序结构如下:以下是我对应用程序所做的主要更改:

So, I have prepared my MERN app for deployment by following the instructions in a Udemy course. My app structure is as follows: The following are the main changes that I have made to my application:

  1. 因为在生产中不会有 create-react-app 创建的服务器,所以我在 server/index.js 中编写了生产路由逻辑,基本上说对于任何不由 app.use("/api/users",userRoutes) &app.use("/api/download", downloadRoutes),后端服务器将在 client/build 文件夹中提供 index.html 文件,该文件夹是我通过运行以下命令创建的:npm run构建.
  1. Because there will be no server created by create-react-app in production, I have written the production routing logic inside server/index.js that essentially says that for any routes not managed by app.use("/api/users", userRoutes) & app.use("/api/download", downloadRoutes), the backend server will server the index.html file inside the client/build folder, which I have created by running the command: npm run build.

服务器/index.js

server/index.js

const express = require("express");
const path = require("path");
const dotenv = require("dotenv");
const colors = require("colors");
const connectDB = require("./config/db");
const {
  notFound,
  globalErrorHandler,
} = require("./middleware/errorMiddleware");
const userRoutes = require("./routes/userRoutes");
const downloadRoutes = require("./routes/downloadRoutes");

dotenv.config();

connectDB();

const app = express();

app.use(express.json());

app.use("/api/users", userRoutes);
app.use("/api/download", downloadRoutes);

// Routing logic in production

if (process.env.NODE_ENV === "production") {
  app.use(express.static(path.join(__dirname, "/client/build")));

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

app.use(notFound);
app.use(globalErrorHandler);

const PORT = process.env.PORT || 5000;

app.listen(PORT, () => {
  console.log(`Server listening on port ${PORT}`.yellow.bold);
});

  1. 我已将 .env 文件中的 process.env.NODE_ENV 更改为 production.

经过上述更改后,当我运行npm start"时(仅启动后端服务器)(不是同时启动前端和后端服务器的npm run dev")并访问 http://localhost:5000,我应该会看到我的应用程序.相反,我看到了以下错误.

After the above-mentioned changes, when I run "npm start" (starts only the backend server) (not "npm run dev" that concurrently starts both the frontend and backend server) and visit http://localhost:5000, I should see my app. Instead, I see the following error.

我做错了什么?

推荐答案

正如您在错误消息中看到的,Express 正在寻找 server/client/build 中的 index.html 不存在.修正路径.

As you can see in the error message, Express is looking for an index.html inside server/client/build which does not exist. Fix the path.

app.use(express.static(path.join(__dirname, '../client/build')))

这篇关于无法准备我的 MERN 应用程序以进行部署的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆