如何用Visual Studio Code远程调试运行在服务器上的nodejs应用
假設(shè)我有一個(gè)nodejs應(yīng)用,運(yùn)行在AWS - 亞馬遜云平臺(tái)上(Amazone Web Service)。我想用本地的Visual Studio Code來(lái)遠(yuǎn)程調(diào)試服務(wù)器端的nodejs應(yīng)用。
Visual Studio Code的調(diào)試配置里定義了兩種類型,attach和launch。Visual Studio Code的官方文檔對(duì)這兩種調(diào)試啟動(dòng)行為的解釋:
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的意思簡(jiǎn)而言之就是以debug模式啟動(dòng)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的調(diào)試器綁定到一個(gè)已經(jīng)處于運(yùn)行狀態(tài)的應(yīng)用。
因?yàn)槲业男枨笫怯帽镜氐腣isual Studio Code去調(diào)試AWS上正在運(yùn)行的nodejs應(yīng)用,毫無(wú)疑問應(yīng)該選Attach。 點(diǎn)擊debug configuration這個(gè)按鈕:
自動(dòng)彈出存放調(diào)試配置信息的launch.json文件了:
把launch.json的內(nèi)容替換成下面的內(nèi)容:
{// 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}] } 復(fù)制代碼這個(gè)配置文件的含義是告訴Visual Studio Code的調(diào)試進(jìn)程,去連接127.0.0.1:9221上的應(yīng)用調(diào)試進(jìn)程去調(diào)試。
當(dāng)然,最后一步我們還需要將本地的127.0.0.1:9221同AWS上的調(diào)試進(jìn)程使用ssh做一個(gè)綁定。
ssh -i C:\Users\i042416.ssh\KOI.pem -L 9221:localhost:9229 ubuntu@amazonaws.com
一切就緒后,做一個(gè)操作觸發(fā)AWS上nodejs應(yīng)用的執(zhí)行。比如我在AWS上部署了一個(gè)nodejs應(yīng)用,作為我github repository的webhook。每當(dāng)我在這個(gè)倉(cāng)庫(kù)創(chuàng)建issue時(shí),github網(wǎng)站就會(huì)推送一個(gè)事件到我的webhook上去。
現(xiàn)在我創(chuàng)建了一個(gè)名為test create issue的issue,一旦我點(diǎn)了Close按鈕,
這個(gè)issue close事件會(huì)自動(dòng)發(fā)送到我的AWS應(yīng)用,下圖可以看到斷點(diǎn)觸發(fā)了,這樣我就實(shí)現(xiàn)了使用本地的Visual Studio Code遠(yuǎn)程調(diào)試AWS應(yīng)用的目的。
要獲取更多Jerry的原創(chuàng)文章,請(qǐng)關(guān)注公眾號(hào)"汪子熙":
總結(jié)
以上是生活随笔為你收集整理的如何用Visual Studio Code远程调试运行在服务器上的nodejs应用的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 想实现高德/百度示例中 源代码编辑器+效
- 下一篇: 全民学python(01)