如何在Azure Linux Web应用程序上托管Angular [英] How to host an angular on Azure linux web app

查看:78
本文介绍了如何在Azure Linux Web应用程序上托管Angular的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个Angular应用程序,我正在尝试在Azure上使用Linux Web应用程序托管.部署后,我无法加载该网站,因此,经过一些研究,我发现我需要添加index.js文件( http://ecominas-website-qas.azurewebsites.net )

这是我的index.js

  var express = require('express');var服务器= express();var options = {索引:"index.html"};server.use('/',express.static('/home/site/wwwroot',options));server.listen(process.env.PORT); 

我也尝试过按此处所述添加web.config 托管AZure Linux应用程序服务中的角度应用程序,但不会加载index.html页面,并且显示默认的一切正常,但未找到应用程序"页面.

我如何才能使用弯角路线?

谢谢

解决方案

所以,我终于设法使其工作了.经过几天的研究和尝试/错误,这是供将来参考的index.js

  var express = require('express');var path = require('path');var服务器= express();var options = {索引:"index.html",重定向:是的,};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));} 别的 {res.sendFile(path.resolve('index.html'));}});server.listen(process.env.PORT); 

I have this Angular app that I am trying to host using Linux web app on Azure. 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)

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. (See http://ecominas-website-qas.azurewebsites.net)

This is my 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.

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

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);

这篇关于如何在Azure Linux Web应用程序上托管Angular的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆