Created
May 12, 2016 21:48
-
-
Save Peleke/f25f852eb839dbcd3dd2a6c7048c37ee to your computer and use it in GitHub Desktop.
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
/* Lightweight wrapper around JSONPlaceholder promise. | |
*/ | |
"use strict"; | |
const http = require('http'), | |
ROOT = 'jsonplaceholder.typicode.com'; | |
// Path is first c. line arg when standalone. | |
const item = process.argv[3], | |
path = process.argv[2]; | |
// Default params require Node 6.0.0+ | |
function makeRequest (path = "", item = "") { | |
return new Promise(function (resolve, reject) { | |
http.get({ | |
host : ROOT, | |
path : `/${path}/${item}` | |
}, function (response) { | |
if (response.statusCode === 200) { | |
let buffer = ""; | |
response.on('data', function (data) { | |
if (data ) | |
buffer += data; | |
}); | |
response.on('end', function () { | |
resolve(JSON.parse(buffer)); | |
}) | |
} else { | |
reject(`Error making connection: ${response.statusCode}.`); | |
} | |
}) | |
}) | |
} | |
module.exports = makeRequest; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment