如何用Visual Studio Code远程调试运行在服务器上的nodejs应用

网友投稿 685 2022-05-30

假设我有一个nodejs应用,运行在AWS - 亚马逊云平台上(Amazone Web Service)。我想用本地的Visual Studio Code来远程调试服务器端的nodejs应用。

如何用Visual Studio Code远程调试运行在服务器上的nodejs应用

Visual Studio Code的调试配置里定义了两种类型,attach和launch。Visual Studio Code的官方文档对这两种调试启动行为的解释:

The best way to explain the difference between launch and attach is think of a launch configuration as a recipe for how to start your app in debug mode before VS Code attaches to it,

Launch的意思简而言之就是以debug模式启动app。

while an attachconfiguration is a recipe for how to connect VS Code’s debugger to an app or process that’s alreadyrunning.

而Attach的含义是将Visual Studio Code的调试器绑定到一个已经处于运行状态的应用。

因为我的需求是用本地的Visual Studio Code去调试AWS上正在运行的nodejs应用,毫无疑问应该选Attach。

点击debug configuration这个按钮:

自动弹出存放调试配置信息的launch.json文件了:

把launch.json的内容替换成下面的内容:

{ // Use IntelliSense to learn about possible attributes. // Hover to view descriptions of existing attributes. // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 "version": "0.2.0", "configurations": [ { "type": "node", "request": "attach", "name": "Jerry's first debug config", "address": "127.0.0.1", "port": 9221 } ] }

这个配置文件的含义是告诉Visual Studio Code的调试进程,去连接127.0.0.1:9221上的应用调试进程去调试。

当然,最后一步我们还需要将本地的127.0.0.1:9221同AWS上的调试进程使用ssh做一个绑定。

ssh -i C:\Users\i042416.ssh\KOI.pem -L 9221:localhost:9229 ubuntu@amazonaws.com

一切就绪后,做一个操作触发AWS上nodejs应用的执行。比如我在AWS上部署了一个nodejs应用,作为我github repository的webhook。每当我在这个仓库创建issue时,github网站就会推送一个事件到我的webhook上去。

现在我创建了一个名为test create issue的issue,一旦我点了Close按钮,

这个issue close事件会自动发送到我的AWS应用,下图可以看到断点触发了,这样我就实现了使用本地的Visual Studio Code远程调试AWS应用的目的。

Node.js Visual Studio

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:【愚公系列】2022年02月 .NET架构班 007-ABP vNext 领域层优化
下一篇:【愚公系列】2022年01月 MinIO文件存储服务器-window11下的安装
相关文章