2013-09-03 2 views
0

url '/ serverTest'에 대해 localhost : 8000에서 localhost : 5000으로 GET 요청을 보내려고합니다. 소켓 끊기 오류가 발생합니다. 이 문제를 어떻게 해결할 수 있습니까? (요청을 처리 할 서버) 포트 5000에node.js 로컬 서버 포트 5000에서 8000으로 http 요청한 경우 오류가 발생합니다.

서버 :

var express = require("express"), http = require('http'); 
app.get('/serverTest', function(req, res){ 
    //authenticate request 
    //send message 
    res.writeHead(200, {'Content-Type':'application/json'}); 
    res.end(JSON.stringify({message:'hello'})); 
}); 
app.listen(5000, function() { 
    console.log("Listening on " + port); 
}); 

포트 8000 (요청을하게 서버)에 서버 :에서 호스팅 서버에서 얻은

var express = require("express"), http = require('http'); 
var port = 8000; 
app.listen(port, function() { 
    console.log("Listening on " + port); 
    makeACall(); 
}); 


function makeACall(){ 
    var options = { 
     host:'localhost', 
     port: 5000, 
     path: '/serverTest', 
     method: 'GET' 
    }; 

    http.request(options, function(response) { 
     var str = ''; 
     response.on('data', function (chunk) { 
      str += chunk; 
     }); 

     response.on('end', function() { 
      console.log(str); 
     }); 
    }); 
} 

오류 포트 8000 :

events.js:72 
throw er; // Unhandled 'error' event 
^ 
Error: socket hang up 
at createHangUpError (http.js:1442:15) 
at Socket.socketOnEnd [as onend] (http.js:1538:23) 
at Socket.g (events.js:175:14) 
at Socket.EventEmitter.emit (events.js:117:20) 
at _stream_readable.js:910:16 
at process._tickCallback (node.js:415:13) 

답변

1

http.request()을 사용하는 경우 일부 poi 실제로 요청을 보내려면 request.end()으로 전화하십시오.

req.end()이 호출되었다는 점에 유의하십시오. http.request()을 사용하면 요청 본문에 데이터가 기록되지 않은 경우에도 요청이 완료되었음을 알리기 위해 항상 req.end()으로 전화해야합니다.

http.request(options, function(response) { 
    // ... 
}).end(); 

또는 요청 GET와 함께, 당신은 또한 request.end()를 호출합니다 http.get()를 사용할 수 있습니다.

http.get(options, function(response) { 
    // ... 
}); 
관련 문제