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

huangapple go评论95阅读模式
英文:

Node.js: Send GET request via unix socket

问题

我是你的中文翻译助手,以下是翻译好的内容:

我对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("在接收器的else部分" + response.statusCode + body);
    }
});

当我尝试与使用Go语言编写的服务器通信时,显示出HTTP错误:400 Bad Request: malformed Host header

以下命令可以正常工作:

curl -X GET --unix-socket /tmp/static0.sock http://:/volumes/list

不确定我的请求有什么问题。我们需要发送头部吗?我期望得到JSON响应。

英文:

I am new to the node.js. I am trying to setup the client server connection using unix socket, where my client request would be in node.js and server running in the background would be in go.

Client side Code:

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


})

When I try to communicate with the server written in go it is shows the HTTP error: 400 Bad Request: malformed Host header'

The same works with:

curl -X GET --unix-socket /tmp/static0.sock http://:/volumes/list

Not sure what is wrong with my request. Do we need to send the headers? I expecting the JSON response.

答案1

得分: 17

要实现这个而不使用request模块,可以尝试以下代码:

const http = require('http');

const options = {
  socketPath: '/tmp/static0.sock',
  path: '/volumes/list',
};

const callback = res => {
  console.log(`状态码: ${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();

这段代码使用了Node.js的http模块来发送HTTP请求。它创建了一个客户端请求对象clientRequest,并通过http.request方法发送请求。请求的选项包括socketPathpath,分别指定了Unix域套接字路径和请求路径。回调函数callback用于处理响应,包括打印状态码和响应数据。最后,通过调用clientRequest.end()方法发送请求。

英文:

To accomplish this without using the request module, try the following:

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();

答案2

得分: -6

考虑以下示例:

客户端代码:
您还可以在某些代码中进行套接字连接:

<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(){
    // 要执行的代码
  });
</script>

Node.js 服务器端代码:
首先安装 npm,然后执行以下步骤:

  1. npm init
  2. npm install --save express
  3. npm install --save socket
var express = require('express');
var app = express();
var http = require('http');
var server = http.createServer();
var io = require('socket.io').listen(server);
var socket = null;

io.sockets.on('connection', function(soc) {
  socket = soc;
  console.log('socket connected');
});

app.get('/test', function(request, response) {
  var message = "Hello world";
  socket.emit('event', message);
  response.writeHead(200);
  response.write(message);
  response.end();
});

var server = app.listen(8080, '0.0.0.0');

您可以在浏览器中访问并检查。

英文:

Consider the following example :

client side code :
Also you can do socket connection in some code :
<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

&lt;script src=&quot;https://cdn.socket.io/socket.io-1.4.5.js&quot;&gt;&lt;/script&gt;
&lt;script&gt;
  socket = io.connect(&#39;http://www.example.com&#39; , {&#39;force new connection&#39;:true});
  socket.on(&#39;event&#39;, function(){
    // code to execute
  });
&lt;/script&gt;

<!-- end snippet -->

server side code in nodejs :
For this install npm, then
1)npm init
2)npm install --save express
3)npm insatll --save socket

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

var express = require(&#39;express&#39;);
var app = express();
var http = require(&#39;http&#39;);
var server = http.createServer();
var io = require(&#39;socket.io&#39;).listen(server);
var socket = null;

io.sockets.on(&#39;connection&#39;, function(soc) {
  socket = soc;
  console.log(&#39;socket connected&#39;);
});


app.get(&#39;/test&#39;, function(request, response) {
  var message = &quot;Hello world&quot;;
  socket.emit(&#39;event&#39;, message);
  response.writeHead(200);
  reponse.write(message);
  reponse.end();
});

var server = app.listen(8080, &#39;0.0.0.0&#39;);

<!-- end snippet -->

You can hit this from browser a check

huangapple
  • 本文由 发表于 2016年12月16日 12:36:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/41177350.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定