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
how to monitor the network on node.js similar to chrome/firefox developer tools?
提问by Chris Snow
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? 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 调试工具。您的选择包括:
- node-http-proxy as seen in How do I use node.js http-proxy for logging HTTP traffic in a computer?
- middlefiddle written in node.js (but abandoned for 3 years now) https://github.com/mdp/middlefiddle
- mitmproxy - a CLI tool http://mitmproxy.org
- fiddler http://www.telerik.com/fiddler
- and many more - https://www.google.pl/search?q=HTTP+debugger
- node-http-proxy 如如何使用 node.js http-proxy 记录计算机中的 HTTP 流量?
- 用 node.js 编写的 middlefiddle(但现在放弃了 3 年)https://github.com/mdp/middlefiddle
- mitmproxy - CLI 工具http://mitmproxy.org
- 小提琴手http://www.telerik.com/fiddler
- 还有更多 - https://www.google.pl/search?q=HTTP+debugger
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 request
package. 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
回答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时写的