2012-01-17 51 views
3

http.createServer中的http.get()函數沒有響應。Node.js. Http.get()函數沒有響應。

我寫了一個小片段來檢索JSON數據,當用戶發送請求到服務器。這是我的代碼。

var http = require('http'); 
var x = ''; 
http.createServer(function (request,response) { 
    http.get({ 
     host:'query.yahooapis.com', 
     path:'/v1/public/yql?q=select%20*%20from%20yahoo.finance.quotes%20where%20symbol%20in%20(%22YHOO%22%2C%22AAPL%22%2C%22GOOG%22%2C%22MSFT%22)&format=json&env=store%3A%2F%2Fdata tables.org%2Falltableswithkeys&callback=cbfunc' 
    }, function(res){ 
     res.on('data',function(d){ 
      x+=d.toString(); 
      console.log(d.toString()); 
     }) 
    }); 

    response.writeHead(200, {'Content-Type':'text/plain'}) 
    var l = JSON.parse(x.substring(7,x.length-2)); 
    response.end(l.query.results.quote[0].symbol + ''); 
}).listen(8080); 

我收到錯誤:

undefined:0

SyntaxError: Unexpected end of input 
    at Object.parse (native) 
    at Server.<anonymous> (C:\Users\Lenovo\Documents\fetch.js:18:12) 
    at Server.emit (events.js:70:17) 
    at HTTPParser.onIncoming (http.js:1491:12) 
    at HTTPParser.onHeadersComplete (http.js:102:31) 
    at Socket.ondata (http.js:1387:22) 
    at TCP.onread (net.js:354:27) 

至於我的想法。錯誤是由於x =''不是json所以它拋出一個錯誤。但是,當通過調用localhost:8080發送請求時,它應該在控制檯上顯示一些json文本而不是錯誤。我的目標是實時解析股票報價,所以我在請求發出時發出了獲取請求,並將其結果設置爲響應。

我想多一個片段用於獲取數據

var http = require('http'); 
var x = '/v1/public/yql?q=select%20*%20from%20yahoo.finance.quotes%20where%20symbol%20in%20(%22YHOO%22%2C%22AAPL%22%2C%22GOOG%22%2C%22MSFT%22)&format=json&env=store%3A%2F%2Fdata tables.org%2Falltableswithkeys&callback=cbfunc'; 

http.get({ 
    host: 'query.yahooapis.com', 
    path: '' 
},function(res){ 
     res.on('data',function(d){ 
     x+=d.toString(); 
     console.log(d.toString()); 
    }) 
}); 
http.createServer(function(request,response){ 
    response.writeHead(200,{'Content-Type':'text/plain'}) 
    var l=JSON.parse(x.substring(7,x.length-2)); 
    response.end(l.query.results.quote[0].symbol+''); 
}).listen(8080); 

它的工作在控制檯上精細顯示JSON文本,但我想我會得到的數據,一旦當我部署它在服務器上而不是當用戶請求數據。我怎樣才能解決這個錯誤?

+0

在你的第一個例子中,最後一行不應該在那裏? – 2012-01-17 10:48:39

回答

3

node.js是異步的。這意味着http.get將在某個時候返回。您發送http.get請求,然後立即嘗試操作x,您只有寫入http.get請求才能完成操作。

基本上x === ''當你打電話JSON.parse因爲http.get回調直到後來纔會觸發。

var http = require('http'); 
var x = ''; 
http.createServer(function(request, response) { 
    http.get({ 
     host: 'query.yahooapis.com', 
     path: '/v1/public/yql?q=select%20*%20from%20yahoo.finance.quotes%20where%20symbol%20in%20(%22YHOO%22%2C%22AAPL%22%2C%22GOOG%22%2C%22MSFT%22)&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys&callback=cbfunc' 
    }, function(res) { 
     res.on('data', function(d) { 
      x += d.toString(); 
      console.log(d.toString()); 
     }); 

     res.on('end', next); 
    }); 

    function next() { 
     response.writeHead(200, { 
      'Content-Type': 'text/plain' 
     }) 

     var l = JSON.parse(x.substring(7, x.length - 2)); 
     response.end(l.query.results.quote[0].symbol + ''); 
    } 
}).listen(8080); 

這裏最重要的是要等到http.get呼叫已完成,直到您發送的JSON響應。

+0

非常感謝您的回答 – 2012-01-17 15:04:50