简体   繁体   English

如何在不使用ng serve的情况下提供Angular 5应用程序?

[英]How can I serve an Angular 5 app without using ng serve?

I can build and serve an Angular 5 project with ng serve and view it in my browser. 我可以使用ng serve来构建和提供Angular 5项目并在浏览器中查看它。 I'd like to move the files onto a docker container because my final app needs to have a php backend. 我想将文件移动到Docker容器中,因为我的最终应用程序需要具有php后端。

When I move the same file system that runs with ng serve to a docker build and try to navigate to it, I receive a server error. 当我将与ng serve一起运行的相同文件系统移至docker build并尝试导航至它时,收到服务器错误。

I know the container works properly because I can serve PHP files to the browser at the respective localhost port without any issues. 我知道容器可以正常工作,因为我可以在各自的本地主机端口将PHP文件提供给浏览器,而不会出现任何问题。 So it needs to be something with Angular that is causing the error. 因此,必须使用Angular引起错误。

I've noticed that the ng new angular-tour-of-heroes project doesn't have an index.html in the root project directory. 我注意到ng new angular-tour-of-heroes项目在根项目目录中没有index.html。 When I move the one in the src/ folder to the root, I still get nothing in the browser. 当我将src/文件夹中的一个移动到根目录时,在浏览器中仍然看不到任何内容。

How can I serve an Angular app using Docker instead of ng serve ? 如何使用Docker而不是ng serve Angular应用程序?

This answer will be two parts since it sounds like you may not be familiar with the build process. 这个答案将分为两个部分,因为听起来您可能不熟悉构建过程。

Building 建造

This is more general to most JavaScript frameworks. 这对于大多数JavaScript框架而言更为通用。 There is usually a "build" process that will produce the static web application in some way that it can be served by a web server. 通常有一个“构建”过程,它将以某种可以由Web服务器提供服务的方式生成静态Web应用程序。

In the case of Angular, it is ng build . 在Angular的情况下,它是ng build You can learn more about the command at https://github.com/angular/angular-cli/wiki/build . 您可以在https://github.com/angular/angular-cli/wiki/build上了解有关该命令的更多信息。 The ng build command would create a dist directory in your project where the built HTML, CSS< and JavaScript lives. ng build命令将在您的项目中创建一个dist目录,在该目录中将生成HTML,CSS <和JavaScript。 The files in this directory are what you would push to your web server to serve. 该目录中的文件是您要推送到Web服务器上的文件。

Docker 搬运工人

Now that we understand how to get the source of the web application to serve, we want to run it as a container. 既然我们了解了如何获得Web应用程序源的服务,我们希望将其作为容器运行。 Based on your question, I am assuming that you already have a Docker image with a web server. 根据您的问题,我假设您已经具有带有Web服务器的Docker映像。 In this case, you could copy the dist folder to the same location as the existing Dockerfile that builds your web server and add a line to your Dockerfile such as: 在这种情况下,你可以在复制dist文件夹到同一位置与现有Dockerfile是构建Web服务器,并添加一行到您的Dockerfile如:

COPY dist/* /my/webserver/root

If you can provide more information about your existing image, Dockerfile , and environment I could provide a better example of building and producing the Angular application to the final web server image. 如果您可以提供有关现有映像, Dockerfile和环境的更多信息, Dockerfile我可以提供一个更好的示例,以将Angular应用程序生成并生成到最终的Web服务器映像。

Though, you don't necessarily need to serve the Angular application from your PHP application. 但是,您不一定需要通过PHP应用程序来提供Angular应用程序。 If your Angular application is connecting to the PHP application they could still be separate Docker images and containers that are linked together. 如果您的Angular应用程序正在连接到PHP应用程序,它们仍然可以是链接在一起的单独的Docker映像和容器。

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

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