使用Visual Studio Code调试运行在SAP云平台上处于运行状态的nodejs应用

117 阅读1分钟

blogs.sap.com/2020/11/23/…

该nodejs应用的package.json:

{
  "name": "debug-cloud",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "engines": {
    "node": "12.X"
},
  "scripts": {
    "start": "node --inspect index.js",
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "express": "^4.17.1"
  }
}

index.js:

const express = require('express')
const app = express()


app.get('/debug', function(req, res){       
    res.send("Debug endpoint called")
 });
 
// Start server
app.listen(process.env.PORT || 8080, ()=>{})

为了部署这个nodejs应用,需要一个Manifest.yml:

---
applications:
- name: debug-app
  memory: 128M
  random-route: true
  buildpacks:
    - nodejs_buildpack

使用命令行部署应用:

cf login #Perform you login to your cf account -- a demo account works as well

cf push #push your application

给CloudFoundry space启用ssh支持:

cf enable-ssh <app-name>
cf allow-space-ssh <space-name>
cf restage <app-name>

重启应用。

在Visual Studio Code里添加一个launch configuration:

 {
            "type": "node",
            "request": "attach",
            "name": "Attach cloud app ",
            "address": "localhost",
            "port": 9229,
            "localRoot": "${workspaceFolder}",
            "remoteRoot": "/home/vcap/app"
          }

将一个本地端口号绑定到nodejs应用上:

cf ssh <APP_NAME> -N -T -L 9229:127.0.0.1:9229

上述命令行将远端服务器的9229端口绑定到本地计算机的9229端口。

现在就可以开始在本地Visual Studio Code里调试了: