How to download a website and write it to a file using Restler?

I would like to download a site (html) and write it to a .html file using node and restler.

https://github.com/danwrong/Restler/

Their original example is already halfway:

var sys = require('util'), rest = require('./restler'); rest.get('http://google.com').on('complete', function(result) { if (result instanceof Error) { sys.puts('Error: ' + result.message); this.retry(5000); // try again after 5 sec } else { sys.puts(result); } }); 

Instead of sys.puts(result); I will need to save it in a file.

I am confused if I need a Buffer , or if I can write it directly to a file.

+6
source share
1 answer

You can simply use fs.writeFile in node:

 fs.writeFile(__dirname + '/file.txt', result, function(err) { if (err) throw err; console.log('It\ saved!'); }); 

Or a streaming more recommended approach that can handle very large files and is very memory efficient:

 // create write stream var file = fs.createWriteStream(__dirname + '/file.txt'); // make http request http.get('http://example.com/', function(res) { // pipe response into file res.pipe(file); // once done file.on('finish', function() { // close write stream file.close(function(err) { console.log('done'); }); }); }); 
+9
source

Source: https://habr.com/ru/post/948946/


All Articles