linux - 如何在 Azure Linux Web 应用程序上托管 Angular

标签 linux angular azure azure-webapps

我有一个 Angular 应用程序,我正在尝试使用 Azure 上的 Linux Web 应用程序托管它。 部署后,我无法加载网站,所以经过一些研究,我发现我需要添加一个index.js文件( Cannot GET index.html Azure Linux Web App )

这解决了我无法加载网站的问题。但它并没有解决 Angular 路由问题。如果我导航到 Angular 路线并点击刷新,页面不会重定向到index.html,并且我会收到 404 响应。 (参见http://ecominas-website-qas.azurewebsites.net)

这是我的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);

我还尝试按照此处所述添加 web.config Hosting angular application in azure linux app service ,但这不会加载 index.html 页面,并且会显示默认的“一切正常,但未找到应用程序”页面。

如何使 Angular 路线正常工作?

谢谢

最佳答案

所以,我终于让它工作了。经过几天的研究和尝试/错误,这是可供将来引用的index.js

var express = require('express');
var path = require('path');

var server = express();
var options = {
    index: 'index.html',
    redirect: true,
};
server.use(express.static('/home/site/wwwroot', options));
const allowedExt = [
    '.js',
    '.ico',
    '.css',
    '.png',
    '.jpg',
    '.woff2',
    '.woff',
    '.ttf',
    '.svg',
  ];

server.get('*', (req, res) => {
    if (allowedExt.filter(ext => req.url.indexOf(ext) > 0).length > 0) {
        res.sendFile(path.resolve(req.url));
    } else {
        res.sendFile(path.resolve('index.html'));
    }
});
server.listen(process.env.PORT);

关于linux - 如何在 Azure Linux Web 应用程序上托管 Angular,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57257403/

相关文章:

c - 如何让asm跳转到c中的变量地址?

python - 如何使用 setuptools 为 linux 创建 rpm 包

javascript - Google 跟踪代码管理器在我的页面底部创建空按钮

azure - 在Azure Logic App中,如何在http请求中传递参数?

c# - $filter 上关于 "invalid expression"的 Azure 搜索错误是什么?

linux - R中没有 "tm"的词云

windows - 文本文件的元数据

angular - for循环中的Mat菜单: performance issues

javascript - 使用 event.target.id 时抛出的类型 'id' 上不存在属性 'EventTarget'

c# - 我可以在我的 asp.net mvc 站点触发的特定日期运行一次 Azure 函数吗?