gpt4 book ai didi

angular - 在azure linux应用程序服务中托管 Angular 应用程序

转载 作者:行者123 更新时间:2023-12-01 01:41:15 26 4
gpt4 key购买 nike

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

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

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

最佳答案

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

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

这是一个示例index.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);

关于angular - 在azure linux应用程序服务中托管 Angular 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56532386/

26 4 0
文章推荐: react-native - 世博会:UIManager 抛出警告 UIManager
文章推荐: javascript - 获取传单中 WMS 的最小缩放级别
文章推荐: javascript - 当 anchor 在另一个 anchor 内动态创建时,为什么 href 不起作用?
文章推荐: javascript - 如何从