你如何调试node.js应用程序?

时间:2022-12-10 20:54:09

I am working on a node.js app, and occasionally it seems to freeze. I assume this is because the user code thread has frozen. It seems to happen after about 5 minutes of usage, but I have no idea why. Are there any tools that would let you know where it's become deadlocked? Apart from adding logging on every line.

我正在开发一个node.js应用程序,偶尔它似乎会冻结。我认为这是因为用户代码线程已冻结。它似乎发生在大约5分钟的使用后,但我不知道为什么。有没有工具可以让你知道它陷入僵局的地方?除了在每一行添加日志记录。

Updated to add more information....

更新以添加更多信息....

I've added some trace statements and have narrowed it down to the following code:

我添加了一些跟踪语句,并将其缩小到以下代码:

exports.addLocationToRoute = function(req, res) {
    console.log("27");
    console.log(req.body);

    var queryConfig = {
        text: "INSERT INTO route_locations (route_id, location_id, order_id) VALUES ($1, $2, $3);",
        values: [req.params.id, req.body.locationId, req.body.order]
    };

    pg.connect(conString, function(err, client) {
    console.log("28");
...

I see 27 output in trace, but not 28. Is there a way to see why it is frozen between those two points?

我看到27个输出跟踪,但没有28个。有没有办法看到为什么它在这两个点之间被冻结?

update 2:

更新2:

I just tried to reproduce again and it's become frozen at a different point in the code, but at this point it is also calling

我只是尝试再次重现,它在代码中的不同点被冻结,但此时它也在调用

pg.connect(conString, function(err, client) {

2 个解决方案

#1


4  

I'm using JetBrains WebStorm IDE for my Javascript development. It has full Node support, meaning you can set breakpoints and trace your code. However, WebStorm isn't free. You may also look at an older topic on Node debugging: How do I debug Node.js applications?

我正在使用JetBrains WebStorm IDE进行Javascript开发。它具有完整的Node支持,这意味着您可以设置断点并跟踪代码。但是,WebStorm不是免费的。您还可以查看有关节点调试的较旧主题:如何调试Node.js应用程序?

There's an accepted answer about using Chrome Development Tools, but I'd rather followed more popular answer's way of using node-inspector for debugging.

关于使用Chrome开发工具有一个公认的答案,但我更喜欢使用更流行的答案来使用node-inspector进行调试。

#2


0  

You probably want to put node into --debug mode and analyze it with node-inspector.

您可能希望将节点置于--debug模式并使用node-inspector进行分析。

When the code locks on some place, you can pause the execution with the pause-button ( http://pix.am/LYZz/ ) and inspect the stack trace to find locks.

当代码在某个地方锁定时,您可以使用暂停按钮(http://pix.am/LYZz/)暂停执行并检查堆栈跟踪以查找锁定。

#1


4  

I'm using JetBrains WebStorm IDE for my Javascript development. It has full Node support, meaning you can set breakpoints and trace your code. However, WebStorm isn't free. You may also look at an older topic on Node debugging: How do I debug Node.js applications?

我正在使用JetBrains WebStorm IDE进行Javascript开发。它具有完整的Node支持,这意味着您可以设置断点并跟踪代码。但是,WebStorm不是免费的。您还可以查看有关节点调试的较旧主题:如何调试Node.js应用程序?

There's an accepted answer about using Chrome Development Tools, but I'd rather followed more popular answer's way of using node-inspector for debugging.

关于使用Chrome开发工具有一个公认的答案,但我更喜欢使用更流行的答案来使用node-inspector进行调试。

#2


0  

You probably want to put node into --debug mode and analyze it with node-inspector.

您可能希望将节点置于--debug模式并使用node-inspector进行分析。

When the code locks on some place, you can pause the execution with the pause-button ( http://pix.am/LYZz/ ) and inspect the stack trace to find locks.

当代码在某个地方锁定时,您可以使用暂停按钮(http://pix.am/LYZz/)暂停执行并检查堆栈跟踪以查找锁定。