【问题标题】:Can I deploy an angular site to any web hosting provider?我可以将 Angular 站点部署到任何网络托管服务提供商吗?
【发布时间】:2018-08-16 07:44:14
【问题描述】:

我可以将我的 Angular 5 网站托管给任何网络托管服务提供商吗?

如果我执行“ng build --prod”,我可以将 dist 文件夹中的所有内容部署到托管服务提供商吗?还是角度要求您找到特定的网络托管服务?

我尝试使用 Namecheap 托管或 000webhosting,但我想确保它有效。

【问题讨论】:

  • 您可以在任何服务器上托管 Angular 应用程序
  • 基本上每台能够托管静态网站的服务器都足以达到目的。

标签: angular hosting web-hosting namecheap


【解决方案1】:

是的!考虑创建的 dist/ 文件夹是包含 index.html 的静态文件夹。

您可以使用任何网络托管服务在 dist 文件夹中提供 index.html

我正在使用 nodejs 为 dist/ 文件夹提供服务。如果您想执行以下步骤。

在根级创建app.js文件并复制以下代码;

const express = require('express'),
http    = require('http'),
path    = require('path');

const app = express();

app.use(express.static(path.join(__dirname, 'dist')));

app.get('*', (req,res) => {
res.sendFile(path.join(__dirname, 'dist/index.html'));
})

const port = process.env.PORT || '4201';
app.set('port', port);

const server = http.createServer(app);
server.listen(port,() => console.log('Running at port '+port))

然后安装快递

npm install --save express

确保你已经构建了

现在做node app.js

如果您在 AWS Web 服务中也有同样的需求

将您的 dist 发送到 aws 服务器 ec2 实例

安装pm2

现在pm2 start app.js

恭喜您在 aws 中托管了您的应用程序。

【讨论】:

  • 既然 Angular 只是静态的 html、css 和 js 文件,你就不能把它托管在像 S3/Cloudfront 或 Azure 的博客存储/CDN 这样的 CDN 上吗?
【解决方案2】:

您还可以在 Apache 服务器上托管构建文件。我只是将它们复制到 htdocs 目录,它对我有用。

【讨论】:

    猜你喜欢
    • 2013-05-09
    • 2021-02-05
    • 1970-01-01
    • 2013-11-24
    • 2020-05-03
    • 2015-12-18
    • 1970-01-01
    • 2019-02-20
    • 2021-05-13
    相关资源
    最近更新 更多