Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading content from URL with Node.js

Tags:

node.js

I'm trying to read the content from a URL with Node.js but all I seem to get are a bunch of bytes. I'm obviously doing something wrong but I'm not sure what. This is the code I currently have:

var http = require('http');  var client = http.createClient(80, "google.com"); request = client.request(); request.on('response', function( res ) {     res.on('data', function( data ) {         console.log( data );     } ); } ); request.end(); 

Any insight would be greatly appreciated.

like image 633
Luke Avatar asked Jun 09 '11 02:06

Luke


People also ask

How do I read a text file from a URL in node JS?

Or if you don't need to save to a file first, and you just need to read the CSV into memory, you can do the following: var request = require('request'); request. get('http://www.whatever.com/my.csv', function (error, response, body) { if (!


1 Answers

try using the on error event of the client to find the issue.

var http = require('http');  var options = {     host: 'google.com',     path: '/' } var request = http.request(options, function (res) {     var data = '';     res.on('data', function (chunk) {         data += chunk;     });     res.on('end', function () {         console.log(data);      }); }); request.on('error', function (e) {     console.log(e.message); }); request.end(); 
like image 123
user896993 Avatar answered Sep 22 '22 08:09

user896993