Skip to content

Instantly share code, notes, and snippets.

@gouegd
Created January 18, 2016 11:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gouegd/1ef492b0c40432bc6c2c to your computer and use it in GitHub Desktop.
Save gouegd/1ef492b0c40432bc6c2c to your computer and use it in GitHub Desktop.
Grab the HTTPS URL passed as 1st arg, and outputs it as a JSON array, each element representing a line of the response.
#!/usr/bin/env node
"use strict";
const https = require('https');
const linesToJsonArray = data => console.log(JSON.stringify(data.split('\n'), null, 2));
const firstArg = process.argv[2];
const url = firstArg ? firstArg.trim() : null;
if (!url || !url.startsWith('https://')) {
console.error('Please pass an HTTPS URL, with the protocol, as the 1st argument');
process.exit(1);
}
https
.get(url)
.on('response', res => {
let data = '';
res.on('error', err => console.error(err) );
res.on('data', chunk => data += chunk );
res.on('end', () => linesToJsonArray(data) );
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment