2012-11-15 75 views
0

您好我收到以下錯誤,當我試圖從我的node.js插座掛斷錯誤

問題,要求打了一個網址:插座掛斷

我的代碼如下

var express = require('express') 
app = express.createServer(); 
var options = { 
    host:'10.230.125.54', 
    port:8080, 
    path:'/' 
}; 
var http = require('http'); 
app.get('/hitmideation', function(req, response) { 
    var jsonresponse; 
    response.contentType('application/json'); 
    console.log('listening'); 
    var req =http.request(options, function(res) { 
     console.log('STATUS: ' + res.statusCode); 
     console.log('HEADERS: ' + JSON.stringify(res.headers)); 
     res.setEncoding('utf8'); 
     res.on('data', function (chunk) { 
      console.log('BODY: ' + chunk); 
      jsonresponse = chunk; 
      response.end('json from url:'+ JSON.stringify(jsonresponse)); 
     }); 
    }); 
    req.on('error', function(e) { 
     console.log('problem with request: ' + e.message); 
    }); 
    req.end(); 
}); 
console.log('I will be listening on: 1340'); 
app.listen(1340); 

如何處理這個問題!任何幫助將不勝感激

回答

1

務必在options工作中的正確的網址在瀏覽器中測試它。如果你確認網址響應試試下面的代碼片段檢索內容:

http.get("10.230.125.54:8080", function(res) { 
    res.setEncoding('utf8'); 
    var content = ''; 
    res.on('data', function (chunk) { 
     content += chunk; 
    }); 
    res.on('end', function() { 
     var obj = JSON.parse(content); 
     // do whatever with the obj 
    }); 
}).on('error', function(err) { 
     // Handle the err here 
    }); 
+0

雅肯定會嘗試@Tolga –