针对 Node.js 的 Azure 远程调试

将 Visual Studio Code 调试器连接到在 LinuxAzure App Service 上运行的 Node.js 应用程序。该调试器的使用方式与连接到本地 Node.js 进程时完全相同,包括断点(Breakpoints)和日志点(Logpoints)的使用。

安装扩展

VS Code 的远程调试支持由 Azure App Service 扩展提供,适用于部署到 Azure 的应用程序。

Select App Service extension

安装 Azure App Service 扩展

  1. 通过按下 ⇧⌘X (Windows、Linux 为 Ctrl+Shift+X) 打开扩展视图
  2. 搜索“azure app service”以筛选结果。
  3. 选择 Microsoft Azure App Service 扩展,然后选择 安装 (Install)

连接到 Azure

安装 Azure App Service 扩展后,请注意活动栏(Activity Bar)中新增了一个 Azure 视图。选择该 Azure 视图以打开 Azure App Service 资源管理器。

App Service explorer

在 App Service 资源管理器中选择 登录到 Azure (Sign in to Azure),或从 命令面板 (Command Palette) (⇧⌘P (Windows、Linux 为 Ctrl+Shift+P)) 中选择 Azure: 登录 (Azure: Sign In) 以登录您的 Azure 账户。如果您还没有账户,请选择 创建 Azure 账户... (Create an Azure Account...) 来创建一个 Azure 免费账户,以尝试各种 Azure 服务组合。

提示:如果您还没有将应用程序部署到 Azure App Service,请先按照本教程将 Node.js + MongoDB Web 应用程序部署到 Azure

启动远程调试会话

要为您的应用程序启动远程调试会话,请在 App Service 资源管理器中右键单击您的应用程序,然后选择 启动远程调试 (Start Remote Debugging)

Start remote debugging

此过程要求在启用调试器的情况下重启应用程序。系统会提示您确认重启。

重启后,VS Code 将通过 SSH 隧道连接到应用程序的调试端口。建立连接可能需要一些时间。连接成功后,VS Code 将切换到调试模式,其工作方式与在本地调试应用程序时相同。

Remote breakpoint

当您准备结束远程调试会话时,请像往常一样从调试器断开连接,并确认您希望重启应用程序。

后续步骤

  • 日志点 (Logpoints) - 使用日志点可以将信息记录到控制台,而无需在调试器中“中断”执行。
  • Azure 扩展 - VS Code Marketplace 包含数百个用于 Azure 和云的扩展。
  • 部署到 Azure - 逐步了解如何将您的应用程序部署到 Azure。
© . This site is unofficial and not affiliated with Microsoft.