简体   繁体   中英

How to monitor outgoing HTTP requests in Node.js?

I'm building a small developer tool (HUD) for an app written in node. In the tool, I want to surface all HTTP/HTTPS requests that were made in order to serve that page (to display them to the developer and record their number). Is there a clean way to do so, which does not involve wrapping something like http.request() ? If not, what is the lowest level method that would need to be wrapped in order to log both HTTP & HTTPS? How about for TCP connections?

The answer to this depends on how complicated your code is. If you make 3 or 4 requests for external resources on each request, just manually inserting profiling code there is probably the cleanest and simplest solution.

If you fire many many requests, monkey-patching (wrapping) http.request and https.request can be done quite easily.

patcher = (patchMe) ->
  original = patchMe.request
  patchMe.request = (options, callback) ->
    console.log options.host, options.method # or any other counting, profiling
    original(options, callback)

http = require 'http'
https = require 'https'

patcher(http)
patcher(https)

If you want to patch more than those requests, on the tcp/udp level, you'll unfortunately have to go read the source to figure out which underlying method you could patch, I haven't gone there.

I enjoyed reading the source of nodetime , which monkey-patches the require function to profile any uses of specific functions / modules deeper in your code. Go read that source! :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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