HTTP Response to file with Node


/ Published in: JavaScript
Save to your folder(s)



Copy this code and paste it in your HTML
  1. var http = require('http');
  2. var fs = require('fs');
  3.  
  4. var mySite = 'jonlabelle.com';
  5. var myClient = http.createClient(80, mySite);
  6. var myRequest = myClient.request('GET', '/', { 'host': mySite });
  7.  
  8. myRequest.end();
  9. myRequest.on('response', function(response) {
  10. console.log('STATUS: ' + response.statusCode);
  11. console.log('HEADERS: ' + JSON.stringify(response.headers));
  12.  
  13. response.setEncoding('utf8');
  14.  
  15. response.on('data', function(chunk) {
  16. fs.writeFile(mySite + '.html', chunk);
  17. console.log('BODY: ' + chunk);
  18. });
  19. });

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.