【问题标题】:Unable to get Angular ng serve command to establish an https context无法获取 Angular ng serve 命令来建立 https 上下文
【发布时间】:2018-10-17 02:23:53
【问题描述】:

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

** NG Live Development Server is running on http://localhost:4200 **

** Angular Live Development Server is listening on localhost:4200, open your browser on http://localhost:4200/ **

是否可以在 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 证书的正确配置)。

  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 上下文中运行。

【问题讨论】:

  • 所以我让它使用 CLI V6.2.2 在 https 上工作,我在命令行中看到的唯一区别是我没有“my-app”,我没有“true” " 在 --SSL 之后,我没有使用 --open。我在它给我的消息中确实得到了不匹配,例如** Angular Live Development Server is listening on localhost:4200, open your browser on https://localhost:4200 **
  • 如果你尝试访问localhost:4200,你会得到什么吗?
  • 感谢您的回复!当我使用 localhost:4200 时,应用程序运行正常。但是 URL 是 localhost:4200 如果我从命令行中删除 my-app,使用以下命令: ng serve —-ssl --ssl-cert "./ssl/localhost.crt" --ssl-key "./ssl /localhost.key”报如下错误:Error: Project '---ssl' could not be found in workspace.如果我使用以下内容: ng serve my-app —-ssl --ssl-cert "./ssl/localhost.crt" --ssl-key "./ssl/localhost.key" 出现以下消息:打开浏览器在localhost:4200
  • 我在另一台 Mac 上安装了 Sierra,安装了最新版本的 node 和 ng,以下正确设置了 https 环境: $ ng serve --ssl true --ssl-cert "./ssl/ localhost.crt" --ssl-key "./ssl/localhost.key" 返回到我的 Mac 后(但不做任何更改),使用 @angular/cli 版本 1.0.0-rc.4,节点 7.10.1和 El Capitan,上述命令运行正常。在前 10 天中,该命令不起作用。这种现象是无法理解的。我会继续前进,希望一切都能继续。
  • 启动 Angular 开发服务器的代码位置:.../node_modules/@angular/cli/tasks/server.js 通过放置以下行,可以修改此代码以解决间歇性 https 的问题: if (serveTaskOptions.sslKey === './ssl/localhost.key') serveTaskOptions.ssl = true; // 在以下行之前解决间歇性 https 问题: const serverAddress = url.format({

标签: angular https localhost serve


【解决方案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 to fix intermittent https problem 

在以下行之前:

const serverAddress = url.format({

【讨论】:

  • 在 .../node_modules/@angular/cli/tasks/server.js,你的意思是 serve.js(没有“r”)吗?我有 serve.js 但没有 server.js。
  • if 语句对我来说会返回 false。我刚刚添加了 serveTaskOptions.ssl = true; 而没有 if 语句,它起作用了。
猜你喜欢
  • 2021-08-05
  • 2021-08-07
  • 1970-01-01
  • 1970-01-01
  • 2020-11-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-09
相关资源
最近更新 更多