我是 node.js 的新手。我正在尝试使用 unix 套接字设置客户端服务器连接,我的客户端请求将在 node.js 中,而在后台运行的服务器将在 go 中。
客户端代码:
var request = require('request');
request('http://unix:/tmp/static0.sock:/volumes/list', function (error, response, body) {
if (!error && response.statusCode == 200) {
console.log(body)
} else {
console.log("In else part of the receiver" + response.statusCode + body)
}
}
})
当我尝试与用 go 编写的服务器通信时,它显示 HTTP error: 400 Bad Request: malformed Host header'
同样适用于:
curl -X GET --unix-socket /tmp/static0.sock http://:/volumes/list
不确定我的请求有什么问题。我们需要发送标题吗?我期待 JSON 响应。
最佳答案
要在不使用请求模块的情况下完成此操作,请尝试以下操作:
const http = require('http');
const options = {
socketPath: '/tmp/static0.sock',
path: '/volumes/list',
};
const callback = res => {
console.log(`STATUS: ${res.statusCode}`);
res.setEncoding('utf8');
res.on('data', data => console.log(data));
res.on('error', data => console.error(data));
};
const clientRequest = http.request(options, callback);
clientRequest.end();
关于javascript - Node.js:通过 unix 套接字发送 GET 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41177350/