Reputation: 24596
When developing client side javascript applications, the developer network panel is invaluable for debugging network issues:
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?
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.
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:
Upvotes: 73
Views: 50607
Reputation: 50225
If you're looking for something official / builtin to the browser debugging interface for NodeJS, at least for Chrome, early work has completed in PR inspector: add initial support for network inspection #53593 (related to issue ticket Support Network Inspection #75). You can find release for this experimental feature in the 22.6.0 release notes. It requires a flag- --experimental-network-inspection
. Use like this: $ node --inspect-wait --experimental-network-inspection index.js
. The Chrome devtools side of things is still pending: https://issues.chromium.org/issues/353924015.
Fun fact: Integration in VS Code's builtin JS Debugger extension is also in progress (see Support network inspection #2051).
Upvotes: 4
Reputation: 380
As you can see, the official node has not yet added support for network domains in v8 inspectors
, but this happens to be very necessary for some developers.
And you made a crucial point that the behavior of agents is not always appropriate and usable.
I also hope to provide a network tab for NodeJS debugging in devtools. After conducting various research on various solutions, I ultimately decided to refer to the CDP protocol and some open-source node network tracking solutions to implement it myself.
Now you can use node-network-devtools
package, which will automatically track all requests (currently not supporting websockets, under development), and automatically open the devtool through your Chrome browser, displaying your nodejs network requests on top.
Due to the use of CDP protocol and integration with Chrome devtool, it is equivalent to the experience of browser development.
It is also very simple to use:
pnpm add node-network-devtools
import { register } from 'node-network-devtools'
register()
Due to not frequently speaking in the stackoverflow community, I am unable to upload preview images. You can learn more through NPM's readme.
Upvotes: 0
Reputation: 1335
Use HTTP Toolkit. Install in macOS by executing:
brew install --cask http-toolkit
It will provide instructions for how to intercept node, chrome and others.
Upvotes: 4
Reputation: 8880
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
import nock from 'nock'
nock.recorder.rec({
output_objects: true
})
Upvotes: 3
Reputation: 1242
I also wished for a network tab in devtools for NodeJS Debugging. As it's absent, I used the below package. This tracks all http and https requests from the NodeJs application and shows them in a chrome network tab like UI.
Upvotes: 2
Reputation: 16915
If you only need to see URLs of outgoing traffic and what caused it, You can use 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
Upvotes: 11
Reputation: 924
If you are using a node version earlier than node 8, I'm a big fan of node-inspector:
https://github.com/node-inspector/node-inspector
I believe it has everything you are looking for:
Upvotes: 2
Reputation: 6262
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:
require('request-debug')(request);
(make sure request-debug package is installed)
This will print all the request data to the console.
Upvotes: 3
Reputation: 3214
Use external HTTP Debugging tool. 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.
Upvotes: 12
Reputation: 2828
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.
Upvotes: 2