Simple Node write to file from API example

node v4.9.1
version: 1.0.0
var request = require('request'); var fs = require('fs'); request({ url: '', 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'); }); } });

no comments

    sign in to comment