Node.js:通过unix套接字发送GET请求

我是新的node.js。 我试图设置客户端服务器连接使用unix套接字,其中我的客户端请求将在node.js和服务器在后台运行将在去。

客户端代码:

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) } } }) 

当我尝试与写入的服务器通信时,它显示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(); 

考虑下面的例子:

客户端代码:你也可以在一些代码中做套接字连接:

 <script src="https://cdn.socket.io/socket.io-1.4.5.js"></script> <script> socket = io.connect('http://www.example.com' , {'force new connection':true}); socket.on('event', function(){ // code to execute }); </script>