Created
January 18, 2016 11:03
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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