简体   繁体   English

Visual Studio Code Node.js调试无法正常工作

[英]Visual studio code nodejs debugging not working

i am new to using visual studio code and trying to debugg node application. 我是使用Visual Studio代码并尝试调试节点应用程序的新手。 I have generated the launch.json file and it is as following : 我已经生成了launch.json文件,它如下所示:

"configurations": [
        {
            "type": "node",
            "request": "launch",
            "name": "Launch Program",
            "program": "${workspaceRoot}/server/app.js"
        }
    ] 

and here is the method which is I am trying to debug, it is inside testController.js : 这是我正在尝试调试的方法,它在testController.js内部:

exports.index = function(req, res) {
    res.send('NOT IMPLEMENTED: Site Home Page');
};

I have also added this controller to route and added the route to express app as following : 我还添加了此控制器进行路由,并将路由添加至Express应用程序,如下所示:

test.js test.js

const express = require('express');

    const router = express.Router();

    var test_controller = require('../controllers/testController');

    router.get('/',test_controller.index);

    module.exports = router;

and app.js : 和app.js:

var index = require('./routes/test');
//Add routes middlewares
app.use('/',index);

I have added the breakpoint to exports.index .The problem when I hit the debugging button it directly hits the breakpoint but when I call the same route from Postman to test api, it never hits the breakpoint. 我已经在exports.index添加了断点。当我按下调试按钮时,它直接碰到了断点,但是当我从邮递员调用相同的路由来测试api时,却没有碰到断点。 Any suggestion. 任何建议。

The breakpoints you put on method definitions will hit during the server start while launching, Where as the breakpoints added on the lines within the method will hit when the methods are being called. 您在方法定义上放置的断点将在启动服务器启动时命中,而在方法内的行上添加的断点将在调用方法时命中。

In your case you added breakpoints to exports.index = function(req, res) { instead of res.send('NOT IMPLEMENTED: Site Home Page') ;. 在您的情况下,您向exports.index = function(req, res) {而不是res.send('NOT IMPLEMENTED: Site Home Page')添加断点; When you hit the debugging button its hitting and on calling same method, inside lines are not hitting. 当您点击调试按钮时,它的点击和调用同一方法时,内部的行都不会被点击。 Make sure you added breakpoints to the lines you want to check. 确保在要检查的行上添加了断点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM