一种将 MERN 应用程序部署到 Azure 而不是 Heroku 的方法

A way to deploy a MERN app to Azure instead of Heroku

我想知道如何将我的 mern 应用程序部署到 Azure。我已经谷歌搜索了好几天,这让我非常沮丧。当我构建我的应用程序时,它显然只构建客户端。如何将我的整个项目上传到 Azure 以及 运行 应用程序的客户端和服务器端?

我想避开 Heroku,直接 运行 我的 Azure 项目。

有什么工具可以用来构建吗?

非常感谢!

为了使 MERN 应用程序在 Azure 上运行,您必须配置您的应用程序以通过 express 显示静态反应构建。

编辑您的快递 server.js 并添加:

const path = require("path"); // on top

// Serve Static assests if in production
if (process.env.NODE_ENV === "production") {
  app.use(express.static("client/build")); // change this if your dir structure is different
  app.get("*", (req, res) => {
    res.sendFile(path.resolve(__dirname, "client", "build", "index.html"));
  });
}

然后,将此脚本行添加到您的 express package.json:

"scripts": {
    "start": "node server.js",
    "azure": "NPM_CONFIG_PRODUCTION=false npm install --prefix client && npm run build --prefix client && npm run start"
  }

注意:编辑它以满足您的目录结构。

就是这样,现在将您的应用程序上传到 Azure,并在 Azure 应用程序设置的环境变量中将您的快速端口设置为 80