如何用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做一個綁定。
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應用的目的。
要獲取更多Jerry的原創文章,請關注公眾號"汪子熙":
總結
以上是生活随笔為你收集整理的如何用Visual Studio Code远程调试运行在服务器上的nodejs应用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 想实现高德/百度示例中 源代码编辑器+效
- 下一篇: 全民学python(01)