I am new to Node and trying to request some json data from a server and save to a file. I can receive the data no problem but can't work out how to write to a file once it has received all the data. Do I need a callback or do I need to use http.CreateServer()? Any help with this would be appreciated.
This is what I have so far:
"use strict";
const request = require('request');
const fs = require('fs');
var options = {
url: 'url-to-request-data',
method: 'GET',
accept: 'application/json',
json: true,}
};
// Start the request
request(options, function(error, response, body) {
if (error) {
return console.log(error);
} else {
var path = "~/jsonfile.json";
fs.writeFile(path, body);
}
});