简体   繁体   English

实现Angular Universal的最佳方法

[英]Best way to implement Angular Universal

I suffer a lot in the past with angular apps and social media, so I'm glad to see that Angular Universal is being developed. 过去,我在使用角度应用程序和社交媒体时遭受了很多苦难,因此很高兴看到Angular Universal正在开发中。

Currently I have some apps that are Angular4 as front end, and Java with Spring as backend. 目前,我有一些以Angular4为前端的应用程序,以Spring为后端的Java应用程序。 As far as I know there are some ways to implement Angular Universal here but they seem pretty complex (at least is what I read). 据我所知,这里有一些实现Angular Universal的方法,但它们似乎相当复杂(至少是我读到的内容)。 So I want to know if that is in that way or not... 所以我想知道那是否是这样...

But anyway, my main question here, is because I saw that in order to implement Angular Universal we should have (ideally) to make the backend with nodejs, how to structure these two technologies, I mean... Should I have Angular app as a frontend app and Nodejs app as a totally different backend app (just like Java) where both are connected with web services? 但是无论如何,我在这里的主要问题是,因为我看到为了实现Angular Universal,我们应该(理想地)使用nodejs制作后端,如何构造这两种技术,我的意思是……我应该将Angular应用作为前端应用程序和Nodejs应用程序是完全不同的后端应用程序(就像Java),两者都与Web服务相连? Or should I served Angular4 SPA direcly from Nodejs views? 还是应该从Nodejs视图直接提供Angular4 SPA? And where should I place Angular Universal here? 我应该将Angular Universal放在哪里?

Now that Angular CLI v1.6 is out, there's native support for building Angular Universal into your projects easily using Node.js! 既然Angular CLI v1.6已经发布,就提供了本机支持,可以使用Node.js将Angular Universal轻松地构建到您的项目中! Essentially, you would ng build --prod to create a dist/ folder, and then create a simple node back-end and connect to your dist/ folder containing your front-end code. 本质上,您会先生成--prod来创建dist /文件夹,然后创建一个简单的后端节点,并连接到包含前端代码的dist /文件夹。 This article gives a great step-by-step guide: Angular server-side rendering in Node with Express Universal Engine . 本文提供了出色的分步指南: 使用Express Universal Engine在Node中进行角度服务器端渲染

When you use Angular Universal, it is going to be a single process (Operating system process) that hosts and serves your Angular pages. 当您使用Angular Universal时,它将是托管并提供Angular页面的单个进程(操作系统进程)。

In production you may have multiple such processes behind a load-balancer. 在生产中,负载均衡器后面可能有多个这样的过程。

Your back-end APIs (if developed in Javascript) may be hosted in the same Node server or in separate server. 您的后端API(如果使用Javascript开发)可以托管在同一Node服务器或单独的服务器中。

The Angular Universal setup steps are detailed here in the official documentation. 该角通用设置步骤详细说明这里的官方文档。

However, I had a few wasted hours to find out the following point 但是,我浪费了一些时间来找出以下几点

  • Webpack 3 is not compatible with ts-loader versions higher than 3.5.0. Webpack 3与高于3.5.0的ts-loader版本不兼容。 At the time of developing this, the latest version of Angular CLI is 1.7.2 which uses Webpack 3.*. 在进行此开发时,Angular CLI的最新版本是1.7.2,使用Webpack 3. *。 Hence, while setting up Angular Universal, install ts-config@3.5.0 因此,在设置Angular Universal时,请安装ts-config@3.5.0

Finally, here I have a seed project for Angular Universal. 最后, 这里有一个针对Angular Universal的种子项目。 It uses Vagrant to locally setup the development environment. 它使用Vagrant在本地设置开发环境。 Also, by tweaking an environment variable in your local host machine, you can run it in a production mode in a Docker container. 另外,通过在本地主机中调整环境变量,可以在生产模式下的Docker容器中运行它。 The steps to run it are detailed in the readme file. 自述文件中详细介绍了运行它的步骤。

If you refer to my Dockerfile in the above Github link, its entrypoint reads: 如果您在上述Github链接中引用我的Dockerfile,则其入口点为:

ENTRYPOINT ["pm2-runtime","start","/home/aus/dist/server.js"]

So, you see, it's just a singe command, and your app is up and running at port 4000. Of course you can use other command line parameters to provide memory limit, port and so on. 因此,您看到的只是一个单一的命令,您的应用程序已在端口4000上启动并运行。当然,您可以使用其他命令行参数来提供内存限制,端口等。

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

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