我能让 nodejs 的 --debug 标记等待远程连接吗?

10
我希望可以使用node --debug app.js运行以下脚本,并让node等待远程调试会话启动。我认为app.js脚本在调试程序能够建立连接之前崩溃了。我正在使用一个隧道将端口5858转发到vagrant框,并且我知道这部分是有效的,因为我以前已经成功地使用了完全相同的设置。
这是否可能?代码在afterCheck(err,spam)) //spam is not defined处崩溃,我想找出原因。
var mysql      = require('mysql');
var connection = mysql.createConnection({
    host     : 'localhost',
    user     : 'root',
    password : '123',
    database : 'kommunity',
    debug: ['ComQueryPacket']
});

var akismet = require('akismet').client({ blog: 'deleted', apiKey: 'deleted' });

var selectPosts = "SELECT p.id as id, poster_ip, message, p.poster FROM topics  t\
                  LEFT JOIN posts p on t.first_post_id = p.id WHERE poster_ip != ''";

//var spam = false;

akismet.verifyKey(function(err, verified) {
    if(err) throw err;
    if (verified) {

        console.log('API key successfully verified.');
        connection.connect();

        connection.query(selectPosts, function(err, rows, fields) {
            if (err) throw err;
            var count = 0;
            rows.forEach( function(entry) {

            //console.log('foreach called with count '+count);
                count++;
                //console.log(entry['id']);

                akismet.checkSpam({
                    user_ip: entry['poster_ip'],
                    comment_author: entry['poster'],
                    comment_content: entry['message']

                    }, afterCheck(err,spam))

                });
        });

    }

    else {

        console.log('Unable to verify API key.');
    }

});

var afterDelete = function (err, rows, fields) {

    if (err) throw err;
    console.log('deleted topic '+rows[0]);
    console.log('spam');
    connection.end();

}

var afterCheck = function(err, spam, entry) {

    if(err) throw err;

console.log('after check called and spam is :'+spam);
    var deleteQuery = "DELETE FROM topics , posts  USING topics INNER JOIN posts \
                       WHERE topics.id=posts.topic_id AND topics.id ="
    if(spam) {
        console.log('spam');
        console.log('entry is ' + entry);
        connection.query(deleteQuery + entry.id + ';', afterDelete(err, rows, fields) );
        connection.end();

    } else {

        console.log('Not spam');

    }
}
1个回答

9

来自WebStorm的运行/调试帮助。我知道这是因为在WebStorm中调试我的nodejs应用程序。

使用--debug-brk选项,在启动后应用程序的执行将暂停。此选项允许您调试启动时执行的代码。

使用--debug选项,将执行必须在应用程序启动时执行的代码,然后应用程序等待调试器连接。当您现在不打算调试Node.js,但以后可能需要进行调试时,此选项非常有用。

更新于 2020 年 5 月

使用节点和npm版本--node 10.16.0 --npm 6.9.0,--debug-brk选项会给出一个弃用警告。

(node:1601) [DEP0062] DeprecationWarning: `node --debug` and `node --debug-brk` are invalid. Please use `node --inspect` or `node --inspect-brk` instead.

我们应该使用--inspect和--inspect-brk来完成这个目的。

谢谢。我正在使用PHPStorm,所以过程应该是相同的。然而,我正在进行远程调试,因此我在服务器上运行--debug,而不是在运行PHPStorm的客户端上运行。我尝试在服务器上使用--debug-brk,但似乎它并没有停在我的断点处。 - codecowboy
2
更新 Node 10.16.0:这些选项已重命名为 --inspect--inspect-brk。这是来自 Node 的完整消息:“node --debugnode --debug-brk 无效。请改用 node --inspectnode --inspect-brk。” - Bruno Berisso

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接