简体   繁体   English

如何使用亚马逊s3服务角2

[英]How to serve angular 2 using amazon s3

Is it possible to use amazon s3 to serve angular 2 app without using a dedicated server. 是否可以使用amazon s3来提供角度2 app而无需使用专用服务器。 If so how to proceed ? 如果是这样怎么办?

Yes, you can deploy an application to AmazonS3 but you shouldn't serve it directly from Amazon S3: S3 is a Storing Service, not a Distribution Service . 是的,您可以将应用程序部署到AmazonS3,但不应直接从Amazon S3提供: S3是存储服务,而不是分发服务 That's why you should create a CloudFront distribution for your S3 bucket. 这就是为什么您应该为S3存储桶创建CloudFront分配。

Steps: 脚步:

  1. Build your app with npm run build --prod (be careful of including the --prod option!) 使用npm run build --prod构建你的应用npm run build --prod (小心包括--prod选项!)

  2. Create a CloudFront distribution for your Amazon S3 bucket and set your Default Root Object to index.html 为您的Amazon S3存储桶创建CloudFront分配,并将您的Default Root Object设置为index.html

  3. If you use url rewrite and not hash strategy (your paths look like http://yourwebsite/login and not http://yourwebsite/#/login create a custom error response for your CloudFront distribution with the following: 如果您使用url重写而不是哈希策略(您的路径看起来像http://yourwebsite/login而不是http://yourwebsite/#/login使用以下内容为您的CloudFront分配创建自定义错误响应:

    • HTTP Error Code: 404 HTTP错误代码:404

    • Error Caching Minimum TTL (seconds): 0 错误缓存最小TTL(秒):0

    • Customize Error Response: Yes 自定义错误响应:是的
    • Response Page Path: /index.html 响应页面路径:/index.html
    • HTTP Response Code: 200 HTTP响应代码:200

You need also to be careful when you deploy your application to Invalidate index.html on CloudFront, otherwise an old version is cached and will be served to the client. 将应用程序部署到CloudFront上的Invalidate index.html时,您还需要小心,否则旧版本将被缓存并将提供给客户端。

Please follow my guide for more details . 按照我的指南了解更多详情

It is possible. 有可能的。 But then you should use webpack . 但是你应该使用webpack

After you configure your app to work with webpack, you can npm run build and upload the processed files to S3 as a static website . 配置应用程序以使用webpack后,您可以npm run build并将处理后的文件作为静态网站上载到S3

No, it is not possible to run angular2 using amazon s3 without dedicated server. 不,没有专用服务器就无法使用amazon s3运行angular2。 According to Angular2 Docs , you need at least little server to run those files. 根据Angular2 Docs ,您至少需要很少的服务器来运行这些文件。 What you can do, is to run NodeJs server on E3 or ESB, and deploy your angular2 app there. 你可以做的是在E3或ESB上运行NodeJs服务器,并在那里部署你的angular2应用程序。

Thanks to @Gunter for pointing out - this can't be done without additional command line parameters. 感谢@Gunter指出 - 没有额外的命令行参数就无法做到这一点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM