简体   繁体   English

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

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

When developing client side javascript applications, the developer network panel is invaluable for debugging network issues:在开发客户端 javascript 应用程序时,开发者网络面板对于调试网络问题非常有用:

在此处输入图像描述

How does a developer creating a NodeJS application monitor the network traffic from the nodejs application to a http/https server?创建 NodeJS 应用程序的开发人员如何监控从 nodejs 应用程序到 http/https 服务器的网络流量? For example how to debug the following network traffic?例如如何调试以下网络流量?

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 .有关更多信息,我要调查的问题在这里

EDIT:编辑:

Here are similar questions asking how to do the same thing in other languages:以下是类似的问题,询问如何用其他语言做同样的事情:

Use external HTTP Debugging tool.使用外部 HTTP 调试工具。 Your options include:您的选择包括:

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

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这个答案是在调试帮助版本 0.2.1 时写的

I came to this question looking for something similar but I'm using the request package.我来到这个问题寻找类似的东西,但我正在使用request包。 In this case all you need to do is include this line in your code:在这种情况下,您需要做的就是在代码中包含这一行:

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

(make sure request-debug package is installed) (确保安装了请求调试包)

This will print all the request data to the console.这会将所有请求数据打印到控制台。

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.但是,您最初的问题不是关于服务器端的问题,而是节点代码本身的问题,所以这在这里没有多大用处。

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:我相信它拥有您正在寻找的一切: 在此处输入图片说明

Use HTTP Toolkit.使用 HTTP 工具包。 Install in macOS by executing:通过执行在 macOS 中安装:

brew install --cask http-toolkit

It will provide instructions for how to intercept node, chrome and others.它将提供有关如何拦截 node、chrome 等的说明。

I also wished for a network tab in devtools for NodeJS Debugging.我还希望在 devtools 中有一个用于 NodeJS 调试的网络选项卡。 As it's absent, I used the below package.由于它不存在,我使用了下面的 package。 This tracks all http and https requests from the NodeJs application and shows them in a chrome network tab like UI.这会跟踪来自 NodeJs 应用程序的所有 http 和 https 请求,并在类似于 UI 的 chrome 网络选项卡中显示它们。

Network Activity Viewer网络活动查看器

One easy way is to use nock recorder functionality.一种简单的方法是使用船尾记录器功能。 As you should be stubbing communication for test cases, you probably need this library as a dev dependency anyway.由于您应该为测试用例存根通信,因此您可能无论如何都需要将此库作为开发依赖项。

The following logs all http[s] comms to console, but you can log to file etc. Documentation here https://github.com/nock/nock#recording以下将所有 http[s] 通信记录到控制台,但您可以记录到文件等。此处的文档https://github.com/nock/nock#recording

import nock from 'nock'

nock.recorder.rec({
  output_objects: true
})

You can use grackle_tracking script and view your data with grackle's platform - we just build this for the exact same purpose: https://www.getgrackle.com/libraries#grackle_tracking_overview您可以使用 grackle_tracking 脚本并使用 grackle 的平台查看您的数据 - 我们只是出于完全相同的目的构建它: https://www.getgrackle.com/libraries#grackle_tracking_overview

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

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