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

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

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


当前回答

为了将你的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部署的小博客。按照链接

其他回答

我想说的是,很多在使用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调用重定向到索引。

为了将你的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部署的小博客。按照链接

你可以通过使用Ahead of Time编译器编译来获得最小和最快的加载产品包,并使用rollup来进行树摇/缩小,如angular AOT烹饪书中所示:https://angular.io/docs/ts/latest/cookbook/aot-compiler.html

在之前的回答中说过,这也可以用Angular-CLI来实现,但如果你还没有使用CLI来制作你的应用,你应该按照食谱来做。

我还有一个材料和SVG图表的工作示例(由Angular2支持),它包括一个用AOT烹饪书创建的包。您还可以找到创建bundle所需的所有配置和脚本。点击这里查看:https://github.com/fintechneo/angular2-templates/

我制作了一个快速视频,演示了AoT编译构建与开发环境的文件数量和大小之间的区别。它显示了上面github存储库中的项目。你可以在这里看到:https://youtu.be/ZoZDCgQwnmQ

在Angular CLI中,你可以使用以下命令:

ng build --prod

它生成一个dist文件夹,其中包含部署应用程序所需的所有内容。

如果你没有使用web服务器的经验,我建议你使用Angular to Cloud。你只需要把dist文件夹压缩成zip文件,然后上传到平台上。

要在IIS中部署应用程序,请遵循以下步骤。

第一步:使用ng Build——prod命令构建Angular应用

步骤2:构建后,所有文件都存储在应用程序路径的dist文件夹中。

步骤3:在C:\inetpub\wwwroot中创建一个名为QRCode的文件夹。

第四步:复制dist文件夹的内容到C:\inetpub\wwwroot\QRCode文件夹。

步骤5:使用命令(Window + R)打开IIS管理器,输入inetmgr,单击“确定”。

步骤6:右键单击默认网站,然后单击添加应用程序。

第七步:输入别名“QRCode”,设置物理路径为C:\inetpub\wwwroot\QRCode。

第八步:打开index.html文件,找到行href="\"并删除'\'。

步骤9:现在在任何浏览器中浏览应用程序。

你也可以跟着视频更好地理解。

视频网址:https://youtu.be/F8EI-8XUNZc