Posted By

mecha on 01/16/11


Tagged

javascript node


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

aanoojgmailcom


HTTP Response to file with Node


 / Published in: JavaScript
 

  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  

You need to login to post a comment.