无法获取Angular ng serve命令来建立https上下文

时间:2018-10-17 02:23:53

标签: angular https localhost serve

当我们使用ng serve命令时,在编译过程中会出现以下消息:

** NG Live Development Server is running on http://localhost:4200 ** <====旧版本的@ angular / cli

** Angular Live Development Server is listening on localhost:4200, open your browser on http://localhost:4200/ ** <==== @ angular / cli的最新版本

是否可以在localhost上配置开发服务器以使用https协议?换句话说,以下内容将作为浏览器中的URL正常工作:

https://localhost:4200

我们尝试了如下所示的命令,但仍然导致使用http协议:

ng serve my-app —-ssl true --ssl-cert "./ssl/localhost.crt" --ssl-key "./ssl/localhost.key" --open

注意:

  1. 已按照正确的步骤为Mac El Capitan环境提供了SSL证书(通过使用node.js和express启动具有https URL的服务器,已确认SSL证书的正确配置。) / p>

  2. 在@ angular / cli版本1.0.0-rc.4和6.2.5中都观察到了此结果

  3. 为什么我们无法将localhost开发服务器配置为在https://localhost:4200下运行?

  4. 我们解决此问题的动机是我们正在使用Facebook登录到我们的应用程序,并且现在他们要求您从https页面进行Facebook API调用。我们具有通过https运行的Angular应用的生产版本。但是,在尝试开发和测试代码时,我们使用localhost来运行我们的测试。为此,我们需要测试环境在https上下文中运行。

1 个答案:

答案 0 :(得分:1)

我观察到发出了以下命令:

$ ng serve --ssl true --ssl-cert“ ./ssl/localhost.crt” --ssl-key“ ./ssl/localhost.key”

将间歇性地正确使用https环境。

以下信息已使“间歇性”问题得以解决:

启动Angular开发服务器的代码位置:... / node_modules/@angular/cli/tasks/server.js

可以通过以下行修改此代码以解决间歇性https问题:

if(serveTaskOptions.sslKey ==='./ssl/localhost.key')serveTaskOptions.ssl = true; // kludge解决间歇性的https问题

在以下行之前:

const serverAddress = url.format({