【发布时间】:2021-02-17 09:54:34
【问题描述】:
我正在 Azure 应用服务(插入我的 BitBucket 存储库)上部署一个使用 NodeJS、Express 和 React 的 Web 应用程序,但我没有找到成功。部署失败,或者它声称它成功,但该站点无法访问并且一直超时。当我在 Azure 诊断中检查 Docker 日志时,我注意到以下内容:
2021-02-14T13:41:53.578Z INFO - Initiating warmup request to container mcm-app_0_543ad0c3 for site mcm-app
在读取Waiting for response to warmup request for container mcm-app_0_543ad0c3 的几条日志之后,我得到以下信息:
2021-02-14T13:45:44.205Z ERROR - Container mcm-app_0_543ad0c3 for site mcm-app did not start within expected time limit. Elapsed time = 230.6269687 sec
2021-02-14T13:45:44.236Z ERROR - Container mcm-app_0_543ad0c3 didn't respond to HTTP pings on port: 8080, failing site start. See container logs for debugging.
2021-02-14T13:45:44.299Z INFO - Stopping site mcm-app because it failed during startup.
这就是我的 Express 后端的 server.js 文件的样子:
var express = require("express");
var cors = require("cors");
var app = express();
var path = require("path");
const port = process.env.PORT || 8080;
// Enable CORS and handle JSON requests
app.use(cors());
app.use(express.json());
app.post("/", function (req, res, next) {
// console.log(req.body);
res.json({ msg: "This is CORS-enabled for all origins!" });
});
// Set router for email notifications
const mailRouter = require("./routers/mail");
const readerRouter = require("./routers/reader");
const notificationsRouter = require("./routers/booking-notifications");
app.use("/email", mailRouter);
app.use("/reader", readerRouter);
app.use("/notifications", notificationsRouter);
app.use(express.static("./mcm-app/build"));
app.get("*", (req, res) => {
res.sendFile(path.join(__dirname, "mcm-app", "build", "index.html"));
});
app.listen(port, () => {
// console.log(`Server is running on port: ${port}`);
});
顺便说一句,我客户的package.json 不包含任何proxy 命令。我删除了它。无论如何都会出现错误。
另外,根据一些建议,我已尝试在 Azure 配置中将 WEBSITES_PORT 设置为 8080。但到目前为止没有任何效果。
如果我能得到一些详细的指导来解决这个问题,我将不胜感激。我没有使用 .yml 文件,以防万一。
【问题讨论】:
-
在你的代码中,我在你的代码中看到了
process.env.PORT,所以你的webapp应用实际上使用了端口80 or 443,而你的WEBSITES_PORT是无效的。因为only one port can be exposed in the container,我认为8080无效。 -
你尝试将
const port = process.env.PORT || 8080;修改为const port = 8080;,然后按照截图中的cli命令进行设置。 -
在 azure web app 中,它只支持 80/443 端口。但是你可以像 pic 一样设置它,当你 delpoy 你的 webapp 时,它总是会路由到 8080 端口。如果我的评论对你有用,请告诉我。
-
@JasonPan 感谢您的建议。在您发布答案之前,我实际上是使用 Docker Compose 来部署我的测试应用程序。所以现在一切都很好:stackoverflow.com/a/66213919/1536240
-
您可以在下方发布您的答案,并与大家分享您的解决方案,这将帮助很多人。我以前看过很多类似的帖子。 ^-^
标签: node.js azure express azure-devops azure-web-app-service