【问题标题】:How to host an angular on Azure linux web app如何在 Azure linux Web 应用上托管 Angular
【发布时间】:2019-07-30 10:50:59
【问题描述】:

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

这解决了我无法加载我的网站的事实。但它不能解决角度路由问题。如果我导航到有角度的路线并点击刷新,页面不会被重定向到 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);

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

我怎样才能让角度路线工作?

谢谢

【问题讨论】:

标签: linux angular azure azure-webapps


【解决方案1】:

所以,我终于设法让它工作了。经过几天的研究和尝试/错误,这是可供将来参考的 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);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-10-25
    • 1970-01-01
    • 2019-03-30
    • 2018-10-09
    • 2021-05-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多