【问题标题】:node, socket.io - connect to list of news feed urls?节点,socket.io - 连接到新闻提要 url 列表?
【发布时间】:2014-11-29 16:08:02
【问题描述】:

我希望通过node 连接到新闻提要网址列表,并通过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)"

有什么方法可以从数组中传递每个新闻提要 url 以通过node 连接它并通过socket.io 获取最新消息???

【问题讨论】:

    标签: node.js sockets


    【解决方案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()

    如果您只想传递整个 URL,则不要使用选项对象,只需像这样传递 URL,该方法将为您解析 URL 到相关部分:

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

    【讨论】:

    • 从您的解决方案中,我能够获取数据。您能否介绍一下如何通过socket.io 向客户端发送数据???
    • @ChakoDesai - 你还没有真正问过关于socket.io 的具体问题。这里有很多例子:socket.io/docs。我不确定你想要什么。如果您需要更多帮助,请为此提出一个新问题,因为它是与获取内容不同的问题。
    • 我已经提出了question
    猜你喜欢
    • 2015-02-02
    • 2015-05-20
    • 2012-11-21
    • 1970-01-01
    • 2020-04-27
    • 1970-01-01
    • 1970-01-01
    • 2011-10-14
    • 2016-07-30
    相关资源
    最近更新 更多