I’ve got a little program that needs to grab favicons from sites using node.js. This works in most cases, but with apple.com, I get errors that I can’t understand or fix:
var sys= require('sys');
var fs= require('fs');
var http = require('http');
var rurl = http.createClient(80, 'apple.com');
var requestUrl = 'http://www.apple.com/favicon.ico';
var request = rurl.request('GET', requestUrl, {"Content-Type": "image/x-icon", "host" : "apple.com" });
request.end();
request.addListener('response', function (response)
{
response.setEncoding('binary');
var body = '';
response.addListener('data', function (chunk) {
body += chunk;
});
response.addListener("end", function() {
});
});
When I make this request, the response is:
<head><body> This object may be found <a HREF="http://www.apple.com/favicon.ico">here</a> </body>
As a result, I’ve modified the above code in just about every way possible with variants of the host name in the client creation step and the url request with ‘www.apple.com’, but typically I just get errors from node as follows:
node.js:63
throw e;
^
Error: Parse Error
at Client.ondata (http:901:22)
at IOWatcher.callback (net:494:29)
at node.js:769:9
Also, I’m not interested in using the google service to grab the favicons.
This code seems to work for me