david-j-davis's notebooks

  • API callback Promises using got - /david-j-davis/api-callback-promises-using-got
    Last edited 7 years ago
    var got = require('got'); var terms = ['Brainf**k', 'Velato', 'Ook!']; var promises = terms.map(term => got('https://api.github.com/search/repositories?q=' + term)); Promise.all(promises).then(function (data) { console.log(data); }).catch(function(error) { console.log(error) });
  • Promises with API callbacks using async-get-json - /david-j-davis/promises-with-api-callbacks-using-async-get-json
    Last edited 7 years ago
    var getJSON = require("async-get-json"); var terms = ['Brainf**k', 'Velato', 'Ook!']; var promises = terms.map(term => getJSON('https://api.github.com/search/repositories?q=' + term)); await Promise.all(promises);
  • Simple Node.js read file example - /david-j-davis/simple-node-js-read-file-example
    Last edited 7 years ago
    var fs = require('fs'); fs.readFile('../build/bundle.js', function(err, data) { if (err) console.log('There was an issue trying to read the file', err); else console.log(data.toString()); }); fs.watch('../app', function (event, filename) { console.log(event); console.log(filename); });
  • Simple Node.js write to file from API example - /david-j-davis/simple-node-write-to-file-from-api-example
    Last edited 7 years ago
    var request = require('request'); var fs = require('fs'); request({ url: 'https://api.github.com/search/repositories?q=node', headers: { 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.87 Safari/537.36' } }, function (error, response, body) { if (error) console.error('error: ', error); if (response.statusCode !== 200 ) console.log('There was an error with the request: ', response.statusCode); else { var obj = JSON.parse(body); console.log(obj.items.length); fs.writeFile('data.json', body, function (err) { if (err) console.log('There was a problem writing to the file: data.json'); else console.log('Successfully saved data to data.json'); }); } });