Group HTTP(S) URLs by Hostname from a Local Text File
JavaScript
Read a UTF-8 text file or standard input, parse each nonempty line as an absolute HTTP(S) URL, and print a JSON object of hostname counts. Reject malformed lines without fetching any URL.
This Node.js 22 CommonJS recipe counts the hostnames in a newline-delimited list of URLs. It accepts a file path or `-` for standard input, ignores blank lines, and reports malformed nonblank lines with their one-based line numbers. It only parses URL text; it never makes network requests.
Before you start
Node.js 22; no external packages are used.
A UTF-8 newline-delimited text file, or a stream piped to stdin.
Each nonblank line must contain one absolute HTTP or HTTPS URL. Leading and trailing whitespace on a line is ignored.
Build and check
Save the CommonJS code to `url-host-summary.js`.
Put one absolute HTTP(S) URL on each line of a text file, or prepare input on stdin.
Run `node url-host-summary.js <file-path>` or `node url-host-summary.js - < <file-path>`.
Inspect the hostname-count JSON on stdout. Correct any rejected lines using the line numbers reported on stderr, then run again.
Limits to understand
Blank lines are ignored; comments and other nonblank text are treated as URLs and rejected if invalid.
The WHATWG URL parser defines normalization, including lowercasing hostnames and internationalized-domain handling. Counts do not include ports or distinguish HTTP from HTTPS.
This reads the entire input into memory and is intended for reasonably sized local text files.
If any nonblank line is invalid or uses a non-HTTP(S) scheme, the command reports errors and produces no count output.
The file contents are treated only as data. The program does not fetch, resolve, or otherwise contact any URL.
What this example adds
Provides a configurable command-line workflow for local URL lists, including stdin support, line-numbered validation errors, deterministic hostname ordering, and JSON output. It counts hostnames rather than full origins, so ports do not create separate groups. No URL is fetched.
Run it
Save the code as `url-host-summary.js`. Run `node url-host-summary.js urls.txt` to read a UTF-8 file, or `node url-host-summary.js - < urls.txt` to read stdin. The program prints JSON to stdout; input and validation failures are written to stderr and exit nonzero. Intended runner: Node.js 22.
Code license: MIT. Keep the license notice with your copy.
Example version 0.1.0 · Documentation reviewed 2026-10-12.
Executed 5 synthetic local fixture cases in isolated Node.js 22.23.3 on GitHub Actions, without network access. Declared exit codes, output assertions and input integrity checks passed. Live services, installed third-party packages and production behavior remain outside scope. Editorial approval is still required.
Related browser tools
Inspect related data locally in your browser. These tools have their own input rules and limits; they do not execute this recipe.