0

我不断收到来自我在 node.js 服务器文件中调用的 URL 的错误请求错误。有任何想法吗?

我也尝试过外部工作 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(){}); 

});
4

1 回答 1

2

尝试在选项对象/的路径字段中添加斜杠“ ”字符:http.get

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

res.end();您的 http 服务器中也缺少您:

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);
...
于 2011-05-14T18:57:55.153 回答