1

如何通过 nodejs 下的 unix 套接字域连接到 LXD API?我尝试以下代码但失败了。我得到了这个答案:HTTP/1.1 400 Bad Request

var net = require('net');

var lxdUSD='/var/lib/lxd/unix.socket';



var net = require('net');
var client = net.connect({path: lxdUSD},
            function() { //'connect' listener
                console.log('connected to socket!: '+lxdUSD);

});

client.on('data', function(data) {
    console.log(data);
    console.log(data.toString());
    client.end();
});

client.on('end', function() {
    console.log('disconnected from socket!: '+lxdUSD);
});

client.on('error', function() {
    console.log('Error');
    process.exit(-1);
});
setInterval(function(){
            var msg="GET /1.0/containers HTTP/1.1\r\n";
            console.log("write to socket: "+msg);
            client.write(msg);
            console.log("........");

}, 15*1000);

从命令行尝试以下操作:

echo -e "GET /1.0/containers HTTP/1.1\r\n" | socat unix-connect:/var/lib/lxd/unix.socket STDIO

输出:

HTTP/1.1 200 OK
Content-Type: application/json
Date: Thu, 15 Oct 2015 15:02:59 GMT
Content-Length: 135

{"type":"sync","status":"Success","status_code":200,"metadata":["/1.0/containers/pc01","/1.0/containers/pc02","/1.0/containers/pc03"]}
4

1 回答 1

1

固定的:

代替

var msg="GET /1.0/containers HTTP/1.1\r\n";

一定是:

var msg="GET /1.0/containers HTTP/1.1\r\n\r\n";
于 2015-10-15T20:44:09.070 回答