node,socket.io - 连接到新闻源网址列表?

时间:2014-11-29 07:14:31

标签: node.js sockets

我希望通过node连接到新闻Feed网址列表,并通过socket.io获取实时数据。为此我尝试使用server.js中的单个网址,如下所示:

var http = require("http");
var options = {
    host: 'http://economictimes.feedsportal.com/c/33041/f/534037/'
};

http.get(options, function (http_res) {
    // initialize the container for our data
    var data = "";

    // this event fires many times, each time collecting another piece of the response
    http_res.on("data", function (chunk) {
        // append this chunk to our growing `data` var
        data += chunk;
    });

    // this event fires *one* time, after all the `data` events/chunks have been gathered
    http_res.on("end", function () {
        // you can use res.send instead of console.log to output via express
        console.log(data);
    });
});

当我执行node server.js时,它会抛出错误

"Error: getaddrinfo ENOTFOUND
    at errnoException (dns.js:37:11)
    at Object.onanswer [as oncomplete] (dns.js:124:16)"

有没有办法从数组中传递每个新闻Feed网址以通过node进行连接,并通过socket.io获取最新消息

1 个答案:

答案 0 :(得分:1)

node doc for the http module开始,这就是典型的选项对象:

var options = {
  hostname: 'www.google.com',
  port: 80,
  path: '/upload',
  method: 'POST'
};

根据文档,您使用的host选项应为A domain name or IP address of the server to issue the request to. Defaults to 'localhost'.因此,您似乎并未正确调用.get()

如果您只想传递整个网址,请不要使用options对象,只需传递这样的网址,该方法会将您的网址解析为相关部分:

http.get('http://economictimes.feedsportal.com/c/33041/f/534037/', function (http_res) {...});