Read File or HTTP
✓ Published0🌍 Public
Mmbostock
Last edited Jul 11, 2012
Created on Jul 11, 2012
This example demonstrates a small utility for reading text content from either a local file or a remote HTTP URL, abstracting the source so callers can use a single `get` function. The code shows a Node.js script that checks the URL protocol: if it starts with `http:`, it uses the `http.get` method to stream the response, collecting data chunks and joining them as a UTF-8 string; otherwise, it falls back to `fs.readFile` with a `"utf8"` encoding. Both paths invoke a callback with either an error or the resulting body, making the helper suitable for loading data files or web resources in server-side visualizations.
AI-generated description