Node.js HTTP Request Metrics Client
The `http-measuring-client` is a Node.js module, currently at version 1.0.1, designed to provide detailed timing statistics for outbound HTTP and HTTPS requests. Released around 2014, the project appears to be unmaintained, with its last copyright year being 2014, indicating an abandoned status. It offers a drop-in replacement for Node's native `http` and `https` modules, emitting a 'stat' event with granular timing data such as `totalTime`, `connectionTime`, `processingTime`, and `transmittingTime` (all in milliseconds) after each request completes. This allows developers to gain insight into network latency and server response times directly from the client without requiring external proxies. While it can integrate with popular HTTP clients like `request` and `superagent` by injecting its custom HTTP module, it also supports a less recommended global monkey-patching approach for broader coverage. Its primary differentiator is the direct integration into the HTTP client layer for metrics collection.
Common errors
-
ReferenceError: require is not defined in ES module scope
cause Attempting to use `require('http-measuring-client')` in a file that is treated as an ES module (e.g., because of `"type": "module"` in `package.json` or `.mjs` extension).fixRename your file to `.cjs`, or change your `package.json`'s `type` field to `"commonjs"`. Alternatively, use `import { createRequire } from 'module'; const require = createRequire(import.meta.url); const httpMeasuringClient = require('http-measuring-client');` if you must use it within an ESM file. -
TypeError: http.get is not a function (or similar 'not a function' error on http methods)
cause This error can occur if the `http-measuring-client` is not correctly initialized with a valid `http` module, or if a global monkey-patching attempt failed or was overwritten.fixEnsure you are calling `httpMeasuringClient.create()` or `httpMeasuringClient.createSecure()` and assigning the result to a variable that you then use for making requests (e.g., `const http = httpMeasuringClient.create(); http.get(...)`). Verify no other module is interfering with the `http` object.
Warnings
- breaking The `http-measuring-client` package is abandoned and has not been updated since 2014. It is unlikely to be compatible with recent Node.js versions, especially those introducing significant changes to the `http` module or new APIs like `fetch` and `http2`. Using unmaintained packages poses security risks and may lead to unexpected behavior.
- gotcha The package is CommonJS-only. Attempting to use `import` statements (ES modules) will result in a `ReferenceError: require is not defined` or similar errors in an ES module context. It must be consumed via `require()`.
- gotcha The documentation explicitly states that monkey-patching the global `http` module using `.mixin(http)` is possible but 'not recommended'. This approach can lead to unforeseen side effects, conflicts with other modules, and make debugging difficult due to altering global behavior.
Install
-
npm install http-measuring-client -
yarn add http-measuring-client -
pnpm add http-measuring-client
Imports
- create
import { create } from 'http-measuring-client';const httpMeasuringClient = require('http-measuring-client'); const http = httpMeasuringClient.create(); - createSecure
import { createSecure } from 'http-measuring-client';const httpMeasuringClient = require('http-measuring-client'); const https = httpMeasuringClient.createSecure(); - mixin
import { mixin } from 'http-measuring-client';const httpMeasuringClient = require('http-measuring-client'); httpMeasuringClient.mixin(require('http'));
Quickstart
const httpMeasuringClient = require('http-measuring-client');
const http = httpMeasuringClient.create();
http.on('stat', (parsedUri, stats) => {
console.log(`Request to ${parsedUri.href} completed.`);
console.log(`Total Time: ${stats.totalTime}ms`);
console.log(`Connection Time: ${stats.connectionTime}ms`);
console.log(`Processing Time: ${stats.processingTime}ms`);
console.log(`Transmitting Time: ${stats.transmittingTime}ms`);
});
// Make an HTTP GET request and collect stats
http.get('http://google.com', (response) => {
let data = '';
response.on('data', (chunk) => {
data += chunk;
});
response.on('end', () => {
console.log('Response from google.com received and ended.');
// console.log('Response body snippet:', data.substring(0, 100) + '...');
});
}).on('error', (err) => {
console.error('Error during HTTP request:', err.message);
});
// Example with an HTTPS request (requires a different client instance)
const https = httpMeasuringClient.createSecure();
https.get('https://example.com', (response) => {
response.on('data', () => {});
response.on('end', () => {
console.log('Response from example.com received.');
});
}).on('error', (err) => {
console.error('Error during HTTPS request:', err.message);
});