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

假设我有一个nodejs应用,运行在AWS - 亚马逊云平台上(Amazone Web Service)。我想用本地的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做一个绑定。

扫描二维码关注公众号,回复: 6191976 查看本文章

ssh -i C:\Users\i042416.ssh\KOI.pem -L 9221:localhost:9229 [email protected]

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

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

 

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

 
 

要获取更多Jerry的原创文章,请关注公众号"汪子熙":

 

猜你喜欢

转载自jerrywang-sap.iteye.com/blog/2440848
今日推荐