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

问题描述 投票:0回答:2

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

我想避免使用 Heroku 并直接从 Azure 运行我的项目。

我可以使用任何工具来构建吗?

提前非常感谢!

reactjs azure deployment mern
2个回答
5
投票

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

编辑您的表达

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


0
投票

这非常有帮助。 但是应该如何从应用程序本身调用端点呢?我不断收到 404 :(

© www.soinside.com 2019 - 2024. All rights reserved.