This is my little piece of code:
var options = {
host: 'www.cuantocabron.com',
port: 80,
path: '/index.php',
method: 'GET'
};
var http = require('http');
var req = http.request(options, function(res) {
console.log('STATUS: ' + res.statusCode);
console.log('HEADERS: ' + JSON.stringify(res.headers));
res.setEncoding('utf8');
res.on('data', function (chunk) {
console.log('BODY: ' + chunk);
});
});
// write data to request body
req.write('data\n');
req.write('data\n');
req.end();
I'm trying to get all the HTML code from that URL; the firts time that I run that code, the console prints all the HTML, but after that, throw an error, so my server goes down.
But, when I run it, the console throw an Error:
events.js:71
throw arguments[i]; //Unhandled 'error' event
Error: Parse Error
at Socket.socketOnData (http.js:1447:20)
at TCP.onread (net.js:404:27)
Any solution? Thanks;
If you are only trying to get the HTML from that URL, you do not need the req.write statements. Those are the statements that are causing your error. You use req.write when you are writing data to the server, E.G. If you were doing a POST.
You should also add an on error handler after you declare your req
req.on('error', function(e) {
console.log('problem with request: ' + e.message);
});