React App 未在 azure 应用服务中启动

Bat*_*man 8 javascript azure reactjs azure-app-service-plans azure-web-app-service

我已将一个简单的 React 应用程序部署到 azure 应用程序服务,但它无法启动:

如何让应用程序运行 index.html?

在此处输入图片说明

小智 20

在您的 azure 仪表板 > 配置 > 启动命令中添加此命令

pm2 serve /home/site/wwwroot --no-daemon

并重新启动您的服务器。这为我修好了!


Sat*_*esh 13

您不需要像其他答案中提到的那样安装 express 和配置 index.js,因为这需要更改配置并且不确定应用程序缩放事件是否会在新实例中保留这些安装。

简单的方法是使用 pm2,因为它已经是堆栈的一部分。为应用程序传递以下启动命令

pm2 serve /home/site/wwwroot --no-daemon
Run Code Online (Sandbox Code Playgroud)

一旦我们重新启动,它应该从 docroot (/home/site/wwwroot) 中选择页面


Mor*_*s S 8

转到 Azure 配置>常规设置

  • 如果您的build文件夹位于项目的根目录

启动命令: pm2 serve /home/site/wwwroot --no-daemon --spa

  • 如果您的build文件夹在您的客户端文件夹内,只需添加路径

启动命令: pm2 serve /home/site/wwwroot/client/build --no-daemon --spa


笔记:

确保您使用 Azure 应用服务linux服务器。

--spa在最后添加了修复 react-router 重定向的问题,Using--spa会自动将所有查询重定向到 index.html,然后 react-router 会发挥它的魔力。


Geo*_*hen 5

如果您部署到 Node Linux Web App,则默认文档将hostingstart.html位于/home/site/wwwroot/.

根据这个

创建 Node.js 应用程序时,默认情况下,它将使用 hostsstart.html 作为默认文档,除非您将其配置为查找不同的文件。您可以使用 JavaScript 文件来配置您的默认文档。在您站点的根文件夹中创建一个名为 index.js 的文件

因此,转到您的 ssh 终端,导航到/home/site/wwwroot. 使用以下代码在那里创建 index.js:

var express = require('express');
var server = express();
var options = {
index: 'index.html'
};
server.use('/', express.static('/home/site/wwwroot', options));
server.listen(process.env.PORT);
Run Code Online (Sandbox Code Playgroud)

注意:一定要在这个文件夹中运行 npm install --save express 否则你的应用服务会在启动时崩溃

重新启动,它会将 index.html 配置为您的应用程序的默认文档。