我有这个Angular应用程序,我正在尝试在Azure上使用Linux Web应用程序托管.部署后,我无法加载该网站,因此,经过一些研究,我发现我需要添加index.js文件(无法获取index.html Azure Linux Web应用)
这解决了我无法加载我的网站的事实.但这不能解决角度布线问题.如果导航到有角度的路线并单击刷新,则页面不会重定向到index.html,并且得到404响应.(请参见 ecominas-website-qas.azurewebsites )
这是我的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 ecominas-website-qas.azurewebsites)
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
发布评论