pm2 serve /home/site/wwwroot --no-daemon
然后重新启动服务器。这对我有用!--proxy-config proxy.conf.json
? - indranil9286由于配置更改可能不会被新实例保留,因此您无需像其他答案中提到的那样安装express并配置index.js文件。
简单的方法是使用pm2,因为它已经是堆栈的一部分。将以下启动命令传递给应用程序即可。
pm2 serve /home/site/wwwroot --no-daemon
一旦我们重新启动,它应该从文档根目录 (/home/site/wwwroot) 恢复页面。
--proxy-config proxy.config.json
? - indranil9286进入Azure配置 >常规设置
build
文件夹位于项目根目录下启动命令:pm2 serve /home/site/wwwroot --no-daemon --spa
build
文件夹位于客户端文件夹中,只需添加路径即可启动命令:pm2 serve /home/site/wwwroot/client/build --no-daemon --spa
注:
确保您使用Azure App Service linux
服务器。
我添加了--spa
以解决react-router重定向问题。使用--spa
将自动将所有查询重定向到index.html,然后react-router将运行其魔法。
--proxy-config proxy.conf.json
? - indranil9286var express = require('express');
var server = express();
var options = {
index: 'index.html'
};
server.use('/', express.static('/home/site/wwwroot', options));
server.listen(process.env.PORT);
注意: 确保在此文件夹中运行 npm install --save express,否则您的应用程序服务将在启动时崩溃。
重新启动后,它会将 index.html 配置为您的应用程序的默认文档。
$ npm install express
- George Chen感谢 Burke Holland。最简单的方法是创建一个运行构建文件夹
npm run build
然后,您将构建文件夹复制到目标位置并添加一个名为“ecosystem.config.js”的文件。
module.exports = {
apps: [
{
script: "npx serve -s"
}
]
};
react-router
,Azure App Service Linux Host 使用 HTTP 服务器将您的代码代理到网络上。 您需要设置 URL 重写规则。 这是因为如果您有一个react-router
url (/about
),当用户刷新页面时,HTTP 服务器将尝试加载一个名为/about
的文件,但该文件不存在,这将导致 404 错误。 URL 重写规则将更改此行为。 它将加载index.html
,/about
将显示来自react-router
的正确页面。 这里是答案的链接:link。 - technogeek1995