在 Azure Linux 应用服务中托管角度应用程序



我正在使用角度框架来构建前端应用程序。有什么方法,如何将应用程序部署到 Azure Linux 应用程序服务?

我已经创建了带有NodeJS堆栈的Web App,并将其分配给Linux App Service。我已经使用命令ng build --prod构建了我的角度应用程序,并将其部署到此 Web 应用程序。当我使用 url: 打开网络浏览器时https://<web-app-name.azurewebsites.net/我能看到的是默认的 html 页面,而不是我的index.html

我正在考虑在 Azure 存储上使用静态网站,但我发现,每个 Azure 存储只能有一个静态网站,但假设我有 10 个静态网站。因此,我不需要创建 10 个 Azure 存储帐户。

您仍然看到默认页面的原因是服务器不知道查看索引.html这是 Angular 应用程序的入口点。您需要在 Angular 应用程序中创建一个 index.js 文件,然后将其包含在 angular.json 的资产部分中。

"assets": [
              "src/favicon.ico",
              "src/assets",
              "src/index.js"
            ],

下面是一个示例索引.js文件,它还包括从非 www 域重定向到 www 域:

// Imports
var express = require('express');
var path = require('path');
// Node server
var server = express();
// When you create a Node.js app, by default, it's going to use hostingstart.html as the 
// default document unless you configure it to look for a different file
// https://blogs.msdn.microsoft.com/waws/2017/09/08/things-you-should-know-web-apps-and-linux/#NodeHome
var options = {
    index: 'index.html'
};
// Middleware to redirect to www
server.all("*", (request, response, next) => {
    let host = request.headers.host;
    if (host.match(/^www..*/i)) {
        next();
    } else {
        response.redirect(301, "https://www." + host + request.url);
    }
});
// This needs to be after middleware configured for middleware to be applied
server.use('/', express.static('/home/site/wwwroot', options));
// Angular routing does not work in Azure by default
// https://stackoverflow.com/questions/57257403/how-to-host-an-angular-on-azure-linux-web-app
const passthroughExtensions = [
    '.js',
    '.ico',
    '.css',
    '.png',
    '.jpg',
    '.jpeg',
    '.woff2',
    '.woff',
    '.ttf',
    '.svg',
    '.eot'
];
// Route to index unless in passthrough list
server.get('*', (request, response) => {
    if (passthroughExtensions.filter(extension => request.url.indexOf(extension) > 0).length > 0) {
        response.sendFile(path.resolve(request.url));
    } else {
        response.sendFile(path.resolve('index.html'));
    }
});
server.listen(process.env.PORT);

最新更新