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

查看:63
本文介绍了无法准备我的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.

server/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. 我已将process.env.NODE_ENV更改为.env文件中的生产版本.

在上述更改之后,当我运行"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天全站免登陆