一旦Angular应用进入生产阶段,你该如何部署它们呢?

到目前为止我看到的所有指南(甚至在angular.io上)都依赖于一个lite-server来服务和browserSync来反映变化——但是当你完成开发时,你如何发布应用程序呢?

我是否导入index.html页面上所有已编译的.js文件,还是使用gulp缩小它们?它们会起作用吗?在生产版本中,我是否需要SystemJS ?


当前回答

简单的答案。使用Angular CLI并发出

ng build 

命令在项目的根目录下。站点将在dist目录中创建,您可以将其部署到任何web服务器。

这将用于测试,如果你在你的应用程序中有生产设置,你应该使用

ng build --configuration production

这将在dist目录中构建项目,并且可以将其推送到服务器。

自从我第一次发布这个答案以来,发生了很多事情。CLI终于在1.0.0,所以按照这个指南去升级你的项目应该在你尝试构建之前发生。https://github.com/angular/angular-cli/wiki/stories-rc-update

其他回答

使用Angular CLI,这很简单。以Heroku为例:

创建Heroku帐号并安装CLI 移动angular-cli dep到package中的依赖项。json(这样当你推送到Heroku时它就会被安装。 添加一个postinstall脚本,当代码被推送到Heroku时将运行ng build。还要为将在下面的步骤中创建的Node服务器添加启动命令。这将把应用程序的静态文件放在服务器上的dist目录中,然后启动应用程序。

"scripts": {
  // ...
  "start": "node server.js",
  "postinstall": "ng build --aot -prod"
}

创建一个Express服务器来服务应用程序。

// server.js
const express = require('express');
const app = express();
// Run the app by serving the static files
// in the dist directory
app.use(express.static(__dirname + '/dist'));
// Start the app by listening on the default
// Heroku port
app.listen(process.env.PORT || 8080);

创建一个Heroku远程和推动部署应用程序。

heroku create
git add .
git commit -m "first deploy"
git push heroku master

下面是我做的一个快速的书写,有更多的细节,包括如何强制请求使用HTTPS,以及如何处理PathLocationStrategy:)

我想说的是,很多在使用angular之前就有Web经验的人,都习惯于在战争中部署他们的Web构件(例如,在Java/Spring项目中部署jquery和html)。在试图将我的angular项目和REST项目分开后,我最终这样做是为了解决CORS问题。

我的解决方案是将所有用CLI生成的angular(4)内容从My -app移到MyJavaApplication/angular。然后我修改了我的Maven构建,使用Maven -resources-plugin将内容从/angular/dist移动到我的发行版的根目录(即$project.build.directory}/MyJavaApplication)。Angular默认从war的根目录加载资源。

当我开始在我的angular项目中添加路由时,我进一步修改了maven build,将index.html从/dist复制到WEB-INF/app。并且,添加了一个Java控制器,将所有服务器端rest调用重定向到索引。

Angular 2在Github Pages中的部署

测试Angular2 Webpack在ghpages中的部署

首先从dist文件夹中获取所有相关文件,对我来说是: + CSS文件在资产文件夹 + main.bundle.js + polyfills.bundle.js + vendor.bundle.js

然后将这些文件推入您创建的repo中。

1 -如果你想让应用程序在根目录上运行-创建一个特殊的repo,名称为[yourgithubusername].github。IO并将这些文件推到主分支中

2——如果你想在子目录或根目录以外的不同分支中创建这些页面,创建一个分支gh-pages并将这些文件推到该分支中。

在这两种情况下,我们访问这些部署页面的方式将是不同的。

对于第一种情况,它将是https://[yourgithubusername].github.io,对于第二种情况,它将是[yourgithubusername].github。io /(回购的名字)。

如果你想使用第二种情况部署它,请确保在dist中更改index.html文件的基本url,因为所有的路由映射都依赖于你给出的路径,它应该设置为[/branchname]。

链接到本页

https://rahulrsingh09.github.io/Deployment

去回购

https://github.com/rahulrsingh09/Deployment

简单的答案。使用Angular CLI并发出

ng build 

命令在项目的根目录下。站点将在dist目录中创建,您可以将其部署到任何web服务器。

这将用于测试,如果你在你的应用程序中有生产设置,你应该使用

ng build --configuration production

这将在dist目录中构建项目,并且可以将其推送到服务器。

自从我第一次发布这个答案以来,发生了很多事情。CLI终于在1.0.0,所以按照这个指南去升级你的项目应该在你尝试构建之前发生。https://github.com/angular/angular-cli/wiki/stories-rc-update

为了将你的Angular2应用部署到生产服务器上,首先要确保你的应用在你的机器上本地运行。

Angular2应用也可以部署为节点应用。

因此,创建一个节点入口点文件server.js/app.js(我的例子使用express)

var express = require('express'),
    path = require('path'),
    fs = require('fs');

var app = express();
var staticRoot = __dirname + '/';

app.set('port', (process.env.PORT || 3000));

app.use(express.static(staticRoot));

app.use(function(req, res, next){

    // if the request is not html then move along
    var accept = req.accepts('html', 'json', 'xml');
    if(accept !== 'html'){
        return next();
    }

    // if the request has a '.' assume that it's for a file, move along
    var ext = path.extname(req.path);
    if (ext !== ''){
        return next();
    }

    fs.createReadStream(staticRoot + 'index.html').pipe(res);

}); 

app.listen(app.get('port'), function() {
    console.log('app running on port', app.get('port'));
});

还要在包中添加express作为依赖项。json文件。

然后将其部署到您喜欢的环境中。

我已经整理了一个关于IIS部署的小博客。按照链接