简体   繁体   English

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

[英]How to host an angular on Azure linux web app

I have this Angular app that I am trying to host using Linux web app on Azure. 我正在尝试使用Azure上的Linux Web应用程序托管此Angular应用程序。 After deployment, I could not load the website so, after a little research, I found that I needed to add a index.js file ( Cannot GET index.html Azure Linux Web App ) 部署后,我无法加载网站,因此,经过一些研究,我发现我需要添加一个index.js文件( 无法获取index.html Azure Linux Web App

That fixed the fact that I could not load my website. 这解决了我无法加载我的网站的事实。 But it does not fix angular routing issue. 但这不能解决角度布线问题。 If I navigate to an angular route and hit refresh, the page does not get redirected to index.html and I get a 404 response. 如果我导航到一条有角度的路线并单击刷新,则该页面不会重定向到index.html,并且得到404响应。 (See http://ecominas-website-qas.azurewebsites.net ) (请参阅http://ecominas-website-qas.azurewebsites.net

This is my index.js 这是我的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);

I have also tried adding a web.config as described here Hosting angular application in azure linux app service , but that would not load the index.html page and the default "everything is working but no app found" page was displayed. 我也尝试过添加web.config,如此处所述在azure linux应用程序服务中托管angular应用程序 ,但这不会加载index.html页面,并且显示默认的“一切正常,但未找到应用程序”页面。

How can I get angular routes working? 我如何才能使用成角度的路线?

Thanks 谢谢

So, I have finally managed to get it working. 因此,我终于设法使其正常工作。 After a few days of research and try/error, this is the index.js that worked for future reference 经过几天的研究和尝试/错误,这是可以用作将来参考的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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM