NodeJS:HTTP响应被触发两次

时间:2014-11-07 18:16:05

标签: javascript json node.js

我从Node JS开始,目前正在做Learnyounode教程。

我进入HTTP模块的第一步:通过http.get()请求获取内容

所以我做了一件非常基本的事情:

var http = require('http')


http.get('http://www.vinylzilla.com/search/autocomplete?q=kiss', function(response){
    response.on('data',function(data){
        console.log("log: ", String(data))
    })
})

这应该返回类似:

log:[  
   {  
      "type":"artist",
      "url":"\/artist\/s-kiss\/533da2a069afa340368bb340",
      "image":"            <img src=\"\/image\/artist\/533da2a069afa340368bb340\/mini\" \/>\n    ",
      "name":"S. Kiss"
   },
   {  
      "type":"artist",
      "url":"\/artist\/\/533da7d669afa340368d4344",
      "image":"    <img src=\"\/img\/artist\/533da7d669afa340368d4344\/mini.jpg\" \/>\n",
      "name":null
   },
   {  
      "type":"artist",
      "url":"\/artist\/kiss-that\/533da92869afa340368dad5a",
      "image":"            <img src=\"\/image\/artist\/533da92869afa340368dad5a\/mini\" \/>\n    ",
      "name":"Kiss That"
   },
   {  
      "type":"artist",
      "url":"\/artist\/killer-s-kiss\/533dbef069afa3545d8d6b4d",
      "image":"            <img src=\"\/image\/artist\/533dbef069afa3545d8d6b4d\/mini\" \/>\n    ",
      "name":"Killer's Kiss"
   },
   {  
      "type":"artist",
      "url":"\/artist\/deep-kiss\/5341a80169afa34f7a8c259b",
      "image":"            <img src=\"\/image\/artist\/5341a80169afa34f7a8c259b\/mini\" \/>\n    ",
      "name":"Deep Kiss"
   },
   {  
      "type":"artist",
      "url":"\/artist\/lajos-kiss\/534f90f369afa38f448c492b",
      "image":"            <img src=\"\/image\/artist\/534f90f369afa38f448c492b\/mini\" \/>\n    ",
      "name":"Lajos Kiss"
   },
   {  
      "type":"artist",
      "url":"\/artist\/kiss-dezso\/535e809c69afa3ce3d8bce90",
      "image":"            <img src=\"\/image\/artist\/535e809c69afa3ce3d8bce90\/mini\" \/>\n    ",
      "name":"Kiss Dezs\u0151"
   },
   {  
      "type":"artist",
      "url":"\/artist\/kiss-kiss-king-kong\/536a21f469afa31c468c96b8",
      "image":"            <img src=\"\/image\/artist\/536a21f469afa31c468c96b8\/mini\" \/>\n    ",
      "name":"Kiss Kiss King Kong"
   },
   {  
      "type":"artist",
      "url":"\/artist\/andy-kiss\/536a289369afa31c468d3822",
      "image":"            <img src=\"\/image\/artist\/536a289369afa31c468d3822\/mini\" \/>\n    ",
      "name":"Andy Kiss"
   },
   {  
      "type":"artist",
      "url":"\/artist\/kiss-kiss-karate-passion\/536a58ef69afa31c46923f8b",
      "image":"            <img src=\"\/image\/artist\/536a58ef69afa31c46923f8b\/mini\" \/>\n    ",
      "name":"Kiss Kiss Karate Passion"
   }
]

而不是我得到:

log:  [{"type":"artist","url":"\/artist\/s-kiss\/533da2a069afa340368bb340","image":"        

    <img src=\"\/image\/artist\/533da2a069afa340368bb340\/mini\" \/>\n    ","name":"S. Kiss"},{"type":"artist","url":"\/artist\/\/533da7d669afa340368d4344","image":"    <img src=\"\/img\/artist\/533da7d669afa340368d4344\/mini.jpg\" \/>\n","name":null},{"type":"artist","url":"\/artist\/kiss-that\/533da92869afa340368dad5a","image":"            <img src=\"\/image\/artist\/533da92869afa340368dad5a\/mini\" \/>\n    ","name":"Kiss That"},{"type":"artist","url":"\/artist\/killer-s-kiss\/533dbef069afa3545d8d6b4d","image":"            <img src=\"\/image\/artist\/533dbef069afa3545d8d6b4d\/mini\" \/>\n    ","name":"Killer's Kiss"},{"type":"artist","url":"\/artist\/deep-kiss\/5341a80169afa34f7a8c259b","image":"            <img src=\"\/image\/artist\/5341a80169afa34f7a8c259b\/mini\" \/>\n    ","name":"Deep Kiss"},{"type":"artist","url":"\/artist\/lajos-kiss\/534f90f369afa38f448c492b",
log:  "image":"            <img src=\"\/image\/artist\/534f90f369afa38f448c492b\/mini\" \/>\n    ","name":"Lajos Kiss"},{"type":"artist","url":"\/artist\/kiss-dezso\/535e809c69afa3ce3d8bce90","image":"            <img src=\"\/image\/artist\/535e809c69afa3ce3d8bce90\/mini\" \/>\n    ","name":"Kiss Dezs\u0151"},{"type":"artist","url":"\/artist\/kiss-kiss-king-kong\/536a21f469afa31c468c96b8","image":"            <img src=\"\/image\/artist\/536a21f469afa31c468c96b8\/mini\" \/>\n    ","name":"Kiss Kiss King Kong"},{"type":"artist","url":"\/artist\/andy-kiss\/536a289369afa31c468d3822","image":"            <img src=\"\/image\/artist\/536a289369afa31c468d3822\/mini\" \/>\n    ","name":"Andy Kiss"},{"type":"artist","url":"\/artist\/kiss-kiss-karate-passion\/536a58ef69afa31c46923f8b","image":"            <img src=\"\/image\/artist\/536a58ef69afa31c46923f8b\/mini\" \/>\n    ","name":"Kiss Kiss Karate Passion"}]

请注意,console.log()会被触发两次,我的响应被拆分为这些日志:为什么

1 个答案:

答案 0 :(得分:2)

“data”事件不保证您将在一个块中从服务器接收整个消息。当收到进入客户端的数据流时,只有那些可立即使用的数据按顺序由您的函数处理。可能会发生整个消息一次全部到达,但行为完全没有确定性。

您可以将整个数据合并为一个字符串,然后将其写入控制台。

var http = require('http')

http.get('http://www.vinylzilla.com/search/autocomplete?q=kiss', function(response){
    var content = '';
    response.on('data',function(data){
        content += String(data);
    });
    response.on('end', function() {
        console.log('Result: ", content);
    });
})