如何托管 Angular 2 网站? [英] how to host angular 2 website?

查看:17
本文介绍了如何托管 Angular 2 网站?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何托管 Angular 2 网站?

How to host angular 2 website?

我是 Angular 2 的新手,我做了一个没有后端的简单网站.

I am new to angular 2 and I made a simple website with no back-end.

我想知道当我尝试直接打开 index.html 文件时,它打开时出错.

I wondered that when I tried to open directly index.html file, it opens with error.

但是在命令 "npm start" 之后它工作正常,它在计算机上运行本地服务器.

But after command "npm start" it works fine, which runs a local server on computer.

那么,如何在简单的托管网站上托管这个网站(不是专用服务器..!)?

So, how to host this website on simple hosting sites (Not a Dedicated Server..!)?

我认为托管网站会自动找到 index.html 文件,但问题是,如果没有 "npm start" 命令,则 index.html 无法启动.

I think hosting sites automatically find index.html file, but here is the problem, index.html is don't start without "npm start" command.

我可以在服务器上启动一个进程吗?

can I have to start an process for that on server?

请指导我.

推荐答案

使用以下简单步骤在 Firebase 上托管您的 Angular 2 应用程序:首先使用 Angular CLI 创建一个项目.在此处获取更多信息 https://cli.angular.io/

Host your Angular 2 App on Firebase using these simple steps: Do Create a project with Angular CLI first. Get More info here https://cli.angular.io/

第 1 步:构建您的应用

运行下面的cmd来构建

Run the below cmd to build

ng build --prod

第 2 步:创建 FireBase 项目并安装 Firebase CLI

https://console.firebase.google.com/ 打开 Firebase 控制台并创建一个新的 Firebase 项目.

Open the Firebase console at https://console.firebase.google.com/ and create a new Firebase project.

要安装 Firebase 命令行工具,请运行:

To install the Firebase command line tools run:

npm install -g firebase-tools

第 3 步:部署到 FireBase

运行以下 firebase cmd 登录:

Run the below firebase cmd to login:

firebase login

它将打开浏览器并要求您进行身份验证.使用您的 Firebase 帐户登录.在那里你可以关闭浏览器窗口.在命令行上,您将收到登录已成功执行的消息.

It will open the browser and ask you for authentication. Login with your Firebase account. There after you can close the browser window. On the command line you'll receive the message that the login has been performed successfully.

现在运行以下命令:

firebase init

首先,系统会询问您要使用哪些 Firebase 客户端功能.您应该选择选项托管:配置和部署 Firebase 托管站点.接下来,Firebase 客户端将询问使用哪个文件夹进行部署.输入 dist.这很重要,因为这是存储我们的生产版本的位置.

First of all you're being asked which of the Firebase client features you want to use. You should select the option Hosting: Configure and deploy Firebase Hosting site. Next the Firebase client will ask which folder to use for deployment. Type in dist. That is important because that is the location where our production build is stored.

接下来的问题是询问此应用程序是否为单页应用程序,以及是否应将所有 URL 重写为 index.html.在我们的例子中,我们需要回答是.

Next the question is ask if this app is a single page app and if it should rewrite all URLs to index.html. In our case we need to answer yes.

最后一个问题是 Firebase 是否应该覆盖 index.html 文件.这个问题的答案是否定的.

Last question is if Firebase should over write file index.html. The answer to that question is no.

现在,运行以下 cmd 进行部署:

Now, Run the below cmd to deploy:

firebase deploy

Firebase 将提供一个 URL,您可以使用它来在线访问您的应用程序.

Firebase will provide a URL which you can use to access your application online.

[更新]

现在,在您成功部署应用后,如果您想进行一些更改并将代码部署在同一 URL 上.遵循相同的程序.但请确保您指向您的项目.

Now after you have successfully deployed your app if you want to make some changes and deploy the code on same URL. Follow the same procedure. But make sure You are pointing to your project.

要列出所有项目,请使用以下命令:

firebase list

要将项目设为当前项目,请使用:

firebase use <project_id>

这篇关于如何托管 Angular 2 网站?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆