Skip to content

Instantly share code, notes, and snippets.

@Peleke
Created May 12, 2016 21:48
Show Gist options
  • Save Peleke/f25f852eb839dbcd3dd2a6c7048c37ee to your computer and use it in GitHub Desktop.
Save Peleke/f25f852eb839dbcd3dd2a6c7048c37ee to your computer and use it in GitHub Desktop.
/* 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