Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

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:

  • PYTHON: How can I see the entire HTTP request that's being sent by my Python application?
  • JAVA: How to enable logging for apache commons HttpClient on Android
like image 822
Chris Snow Avatar asked Mar 05 '15 08:03

Chris Snow


People also ask

How do I inspect node js in Chrome?

Using Google Chrome DevTools to Debug The next step is to head to Chrome, open a new tab, and enter the URL chrome://inspect/ . Click on “Open dedicated DevTools for Node” to start debugging the application. It will take a couple of seconds to view the source code in Chrome DevTools.

Does node js work on Firefox?

So Node. js does not "work on Firefox" (it doesn't work on Google Chrome either): its a server-side technology. Think of it as a replacement for Python/Ruby/Java in that role. So it can/does respond to requests from all sorts of clients (like Google Chrome and Firefox).


2 Answers

Use external HTTP Debugging tool. Your options include:

  • 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

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.

like image 171
OhJeez Avatar answered Sep 29 '22 23:09

OhJeez


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

like image 20
naugtur Avatar answered Sep 29 '22 21:09

naugtur