javascript 如何在类似于 chrome/firefox 开发者工具的 node.js 上监控网络?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28873332/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 09:41:32  来源:igfitidea点击:

how to monitor the network on node.js similar to chrome/firefox developer tools?

javascriptnode.jshttpsgoogle-chrome-devtools

提问by Chris Snow

When developing client side javascript applications, the developer network panel is invaluable for debugging network issues:

在开发客户端 javascript 应用程序时,开发人员网络面板对于调试网络问题非常重要:

enter image description here

在此处输入图片说明

How does a developer creating a NodeJS application monitor the network traffic from the nodejs application to a http/https server? For example how to debug the following network traffic?

创建 NodeJS 应用程序的开发人员如何监控从 nodejs 应用程序到 http/https 服务器的网络流量?例如如何调试以下网络流量?

var http = require('http');
var req = http.request ...
req.write ...
req.send()

My code is making a call to a third party https server, so I am unable to use wireshark or similar packet sniffing tools.

我的代码正在调用第三方 https 服务器,因此我无法使用 wireshark 或类似的数据包嗅探工具。

For more information, the problem I am trying to investigate is here.

有关更多信息,我正在尝试调查的问题是here

EDIT:

编辑:

Here are similar questions asking how to do the same thing in other languages:

以下是类似的问题,询问如何用其他语言做同样的事情:

采纳答案by HeadCode

I know it's not pretty, but you could always output the content of the response headers on the console inside your request call:

我知道这并不漂亮,但是您始终可以在请求调用中的控制台上输出响应标头的内容:

var req = https.request(options, function(res) {
    console.log("statusCode: ", res.statusCode);
    console.log("headers: ", res.headers);

    res.on('data', function(d) {
        process.stdout.write(d);
    });
});

Your original question, however, was not about problems with the server side but rather a problem with the node code itself so this wouldn't be of much use here.

然而,您最初的问题不是关于服务器端的问题,而是节点代码本身的问题,所以这在这里没有多大用处。

回答by OhJeez

Use external HTTP Debugging tool. Your options include:

使用外部 HTTP 调试工具。您的选择包括:

You fire up one of those, tell them where to route the traffic, and point your application at that debugging proxy instead of the real server.

您启动其中之一,告诉他们将流量路由到哪里,并将您的应用程序指向该调试代理而不是真正的服务器。

回答by LucasSeveryn

I came to this question looking for something similar but I'm using the requestpackage. In this case all you need to do is include this line in your code:

我来到这个问题寻找类似的东西,但我正在使用这个request包。在这种情况下,您需要做的就是在代码中包含这一行:

require('request-debug')(request);

require('request-debug')(request);

(make sure request-debug package is installed)

(确保安装了请求调试包)

This will print all the request data to the console.

这会将所有请求数据打印到控制台。

回答by pulekies

If you are using a node version earlier than node 8, I'm a big fan of node-inspector:

如果你使用的节点版本早于节点 8,我是节点检查器的忠实粉丝:

https://github.com/node-inspector/node-inspector

https://github.com/node-inspector/node-inspector

I believe it has everything you are looking for: enter image description here

我相信它拥有您正在寻找的一切: 在此处输入图片说明

回答by naugtur

If you only need to see URLs of outgoing traffic and what caused it, You can use debugging-aid

如果您只需要查看传出流量的 URL 及其原因,您可以使用 debugging-aid

npm i -D debugging-aid
node --require debugging-aid/network app.js 

Resulting console output may look like this:

结果控制台输出可能如下所示:

[aid] network, outgoing  to: http://example.com/
 stack:     at Agent.createSocket (_http_agent.js:234:26)
    at Agent.addRequest (_http_agent.js:193:10)
    at new ClientRequest (_http_client.js:277:16)
    at Object.request (http.js:44:10)
    at Request.start (myapp-path/node_modules/request/request.js:751:32)
    at Request.end (myapp-path/node_modules/request/request.js:1511:10)
[aid] network, outgoing  to: http://example.com/
 stack:     at Agent.createSocket (_http_agent.js:234:26)
    at Agent.addRequest (_http_agent.js:193:10)
    at new ClientRequest (_http_client.js:277:16)
    at Object.request (http.js:44:10)
    at get (myapp-path/node_modules/got/source/request-as-event-emitter.js:234:22)
    at Immediate.<anonymous> (myapp-path/node_modules/got/source/request-as-event-emitter.js:305:10)


Disclaimer:

免责声明:

I'm the author of debugging-aid
This answer was written when debugging-aid was on version 0.2.1

我是debugging-aid的作者
这个答案是在debugging-aid版本0.2.1时写的