使用Visual Studio代码调试ExpressJS服务器端代码

使用Visual Studio代码调试ExpressJS服务器端代码,express,debugging,visual-studio-code,gulp,vscode-debugger,Express,Debugging,Visual Studio Code,Gulp,Vscode Debugger,我已经做了一个简单的CRUD应用程序使用 快递:4.13.4 吞咽:3.9.1 mongodb:v3.0.6 reactjs:15.0.2 节点:4.0.0 对于服务器端代码,我听说可以通过Visual Studio代码(v1.1.1.)进行调试。 在git bash中,我通过gulp serve启动应用程序。但我不知道如何开始调试 我吞咽任务的一小段 gulp.task('serve',['bundle','start-server'],function(){ browserSy

我已经做了一个简单的CRUD应用程序使用

  • 快递:4.13.4
  • 吞咽:3.9.1
  • mongodb:v3.0.6
  • reactjs:15.0.2
  • 节点:4.0.0
对于服务器端代码,我听说可以通过Visual Studio代码(v1.1.1.)进行调试。

在git bash中,我通过
gulp serve
启动应用程序。但我不知道如何开始调试

我吞咽任务的一小段

gulp.task('serve',['bundle','start-server'],function(){

    browserSync.init({
        proxy:'http://localhost:3000',
        port:9001
    });

});
当我们单击VS代码上的调试按钮来启动调试界面时,会显示一个launch.json,其中有两个配置选项

{
"version": "0.2.0",
"configurations": [
    {
        "name": "Launch",
        "type": "node",
        "request": "launch",
        "program": "${workspaceRoot}",
        "stopOnEntry": false,
        "args": [],
        "cwd": "${workspaceRoot}",
        "preLaunchTask": null,
        "runtimeExecutable": null,
        "runtimeArgs": [
            "--nolazy"
        ],
        "env": {
            "NODE_ENV": "development"
        },
        "externalConsole": false,
        "sourceMaps": false,
        "outDir": null
    },
    {
        "name": "Attach",
        "type": "node",
        "request": "attach",
        "port": 3000,
        "address": "localhost",
        "restart": false,
        "sourceMaps": false,
        "outDir": null,
        "localRoot": "${workspaceRoot}",
        "remoteRoot": null
    }
]
}

我猜这些是启动和连接配置。但是,我们如何通过调试实际启动一个大容量的程序呢

我见过有人通过将“程序”键修改为“/usr/local/bin/grunt”来启动grunt流程。但我似乎不能一口吞下

即使我已经通过git bash启动了我的应用程序,并尝试像前面提到的那样“附加”调试器,vs代码也只会显示一条错误消息,说“已取消”

TLDR

  • 在VS代码中启动调试时,如何启动gulp(或)grunt(或)启动服务器
  • 是否可以通过cmd或bash从外部启动应用程序,并且仍然能够使用调试器调试服务器端代码?如果是,launch.json中需要做哪些更改

好吧,后来我终于通过启动和附加成功调试了大量书签和链接

通过启动配置进行调试:

      {
            "name": "Launch",
            "type": "node",
            "request": "launch",
            "program": "${workspaceRoot}/server.js",
            "stopOnEntry": true,
            "args": [],
            "cwd": "${workspaceRoot}",
            "preLaunchTask": null,
            "runtimeExecutable": null,
            "runtimeArgs": [
                "--nolazy"
            ],
            "env": {
                "NODE_ENV": "development"
            },
            "externalConsole": false,
            "sourceMaps": false,
            "outDir": null
        }
       {
            "name": "Attach",
            "type": "node",
            "request": "attach",
            "port": 5858,
            "address": "localhost",
            "restart": false,
            "sourceMaps": false,
            "outDir": null,
            "localRoot": "${workspaceRoot}",
            "remoteRoot": null
        }
在下拉菜单中选择启动选项并按下VSC调试视图上的绿色>按钮时,您应该会在VSC控制台中看到类似的内容

node --debug-brk=21735 --nolazy server.js
调试器应该暂停在server.js文件的第一行。 使用断点进行调试!:)

通过附加配置进行调试:

      {
            "name": "Launch",
            "type": "node",
            "request": "launch",
            "program": "${workspaceRoot}/server.js",
            "stopOnEntry": true,
            "args": [],
            "cwd": "${workspaceRoot}",
            "preLaunchTask": null,
            "runtimeExecutable": null,
            "runtimeArgs": [
                "--nolazy"
            ],
            "env": {
                "NODE_ENV": "development"
            },
            "externalConsole": false,
            "sourceMaps": false,
            "outDir": null
        }
       {
            "name": "Attach",
            "type": "node",
            "request": "attach",
            "port": 5858,
            "address": "localhost",
            "restart": false,
            "sourceMaps": false,
            "outDir": null,
            "localRoot": "${workspaceRoot}",
            "remoteRoot": null
        }
从外部启动服务器

$node --debug-brk server.js
您的提示应该暂停在

Debugger listening on port 5858
按下VSC调试视图上的绿色>按钮,并在下拉列表中选择附加选项,调试器应自动附加自身并在server.js的第一行暂停


调试异常

如果一开始解决方案未连接,请尝试浏览器刷新。另外,VS code在屏幕底部有一个自动连接选项。
outDir
现在不推荐使用“从外部启动服务器”@TrietPham连接程序不启动服务器,您必须手动执行此操作,例如,
npm run start
或类似的东西,这取决于您的脚本。我想这已经在帖子中说过:>$node--debug brk server.js这有意义吗?