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

10

71

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

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?

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:

Olivette answered 5/3, 2015 at 8:37 Comment(9)
what you need is middleware. express comes to mind. you could also globally replace the require('http'); with something like require('./http-log');, where the http-log file is a simple wrapper around the existing http module's exports's methods that logs such details by binding extra events as it returns the original.Admirable
Do you want to investigate the state of a single request or an html page with its dependent resources?Danuloff
@arturgrzesiak I just want to investigate a single request.Olivette
@ChrisSnow what exact information are you after? (It seems that image of chrome's network tab is pretty unrelated to your issue.)Danuloff
@arturgrzesiak I'm trying to investigate why an outgoing https request from my nodejs application is throwing an exception. To investigate it would be useful to see the raw https request and response.Olivette
Sorry, but I do not understand the situation -- you accepted answer to the other question. Using require('https') did not solve your issue? Please specify exactly what is your problem.Danuloff
I have accepted the answer to the other question, but in the future when I have network call issues I would like to be able to debug them for myself. To do that I need to be able to see what is going on at the http layer. Here are similar questions for other languages: PYTHON: #10589144 and JAVA: #3247292Olivette
As far as I can tell, no answers here point to the chrome dev tools for inspecting network requests. node-inspector is deprecated after Node.js v6. All of these other solutions depend on 3rd party libs. I use the chrome inspector for debugging everything in Node.js, except I can't figure out how to do it with network requests.Cautionary
I have installed all the third parties and none of them work ... Other than setting NODE_DEBUG=http as environment variable. Is such a shame for nodejs to leave such a vital inspection offWasherwoman
F
3

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.

Footstalk answered 7/3, 2015 at 17:44 Comment(2)
Unfortunately only the req.on('error', function(e) {} call back is getting executed and not the https.request(options, function(res) {}Olivette
@ChrisSnow If there is such an error there is nothing to see anyway. You won't be getting any headers back.Footstalk
O
12

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.

Oxford answered 11/3, 2015 at 23:6 Comment(3)
fails completely if your traffic is on sslWaldenses
@MuhammadUmer you can install a root certificate for tools such as fiddler and mitmproxy to break TLS connections open.Devotee
@MuhammadUmer Not if you pass environment variable NODE_TLS_REJECT_UNAUTHORIZED=0 to your node app.Hyperesthesia
A
11

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

Appoint answered 24/8, 2019 at 14:52 Comment(2)
I had a similar need and this tool was perfect. Simple, easy to install and use, and it gave me exactly the information I needed -- and more. It told me the network requests being made and also a stack trace of where the request was made. I had the answer I needed in minutes. Thanks!Choler
It didn't work very well for me, partially logged one request (parts of the url just said "undefined") and it missed other requests (which may have been localhost requests, i'm not sure, either way its unreliable)Detergent
U
4

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.

Ulceration answered 5/6, 2015 at 8:13 Comment(0)
C
4

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.

Containment answered 26/3, 2021 at 8:12 Comment(0)
F
3

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.

Footstalk answered 7/3, 2015 at 17:44 Comment(2)
Unfortunately only the req.on('error', function(e) {} call back is getting executed and not the https.request(options, function(res) {}Olivette
@ChrisSnow If there is such an error there is nothing to see anyway. You won't be getting any headers back.Footstalk
F
3

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
})
Frugivorous answered 3/8, 2022 at 16:23 Comment(0)
F
2

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: enter image description here

Fifteenth answered 10/8, 2017 at 18:17 Comment(5)
I'm not sure what you mean @jamrizzi. You are saying node-inspector is missing the network tab? After installing it and running 'node-debug app.js' I definitely have a network tab.Fifteenth
Hmmm, I was confused. node --inspect doesn't give me a network tab.Forespeak
@jamrizzi please note that node-inspector != node --inspect. node-inspector is an external javascript program, while node --inspect is an embedded feature of node runtime.Sensitize
Its missing for me too.Particia
Note: node-inspector was abandoned a few years agoWalleye
S
2

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.

Network Activity Viewer

Siebert answered 7/4, 2022 at 13:50 Comment(0)
O
0

If you're looking for something official / builtin to the browser debugging interface for NodeJS, at least for Chrome, it doesn't exist yet, but there's an open feature-request for it: Support Network Inspection #75. What you can do is give the issue ticket a thumbs up reaction to show that you want it to be implemented (this helps maintainers gauge what users would prefer to be prioritized). Note: Please avoid making noisy comments in the issue ticket such as ones that only consist of "+1" / "bump"; leaving a thumbs up is enough to convey that. You can track the current progress by reading the existing comments and subscribing to the issue ticket to get notified about new comments.

Odontograph answered 16/1 at 7:30 Comment(0)
B
0

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:

  1. Install
pnpm add node-network-devtools
  1. usage
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.

Bladdernose answered 19/6 at 2:30 Comment(1)
gh linK: github.com/GrinZero/node-network-devtoolsOdontograph

© 2022 - 2024 — McMap. All rights reserved.