我如何通过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"]}
最佳答案
固定:
代替
var msg="GET /1.0/containers HTTP/1.1\r\n";
一定是:
var msg="GET /1.0/containers HTTP/1.1\r\n\r\n";
关于node.js - LXD-如何通过Node.js下的Unix套接字域连接到LXD API?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33152103/