2011-05-14 6 views
0

node.js 서버 파일에서 호출하는 URL에서 잘못된 요청 오류가 계속 발생합니다. 어떤 아이디어?node.js의 URL에서 json을 가져올 때 잘못된 요청 400 받기

외부 작동 URL도 시도 했으므로 로컬 호스트 문제가 아닙니다.

var http = require('http'), 
io = require('socket.io'); 

server = http.createServer(function(req, res){ 
res.writeHead(200, {'Content-Type': 'text/html'}); 
}); 
server.listen(8181); 

// socket.io 
var socket = io.listen(server); 

socket.on('connection', function(client){ 

function getMessages() { 

    http.get({ host: 'localhost', port: 8888, path: 'json.php' }, function(response) { 

     var data = ""; 
     response.on('data', function(chunk) { 
      data += chunk; 
     }); 

     response.on('end', function() { 

      socket.broadcast(JSON.parse(data)); 
      //console.log(data); 
      //socket.broadcast('{ "alrt": "YES", "message": "Something is wrong!" }'); 
      //socket.broadcast('hi'); 
     }); 

    }); 

} 

setInterval(getMessages, 1000); 

client.on('message', function(){}) 
client.on('disconnect', function(){}); 

}); 

답변

2

에 한번 당신의 http.get 옵션의 경로 필드에 슬래시 '/'문자를 추가 오브젝트 :

var http = require('http'), 
    io = require('socket.io'); 

server = http.createServer(function(req, res){ 
    res.writeHead(200, {'Content-Type': 'text/html'}); 
    res.end(); 
}); 
server.listen(8181); 
... 
:

또한
... 
http.get({ host: 'localhost', port: 8888, path: '/json.php' }, function(response) { 
... 

는 HTTP 서버에서 res.end(); 누락

관련 문제