Node.js:通过unix套接字发送GET请求 [英] Node.js: Send GET request via unix socket

查看:189
本文介绍了Node.js:通过unix套接字发送GET请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 node.js 的新手。我正在尝试使用unix套接字来设置客户端服务器连接,其中我的客户端请求将位于 node.js 中,并且在后台运行的服务器将继续运行。

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


})

当我尝试与写入的服务器通信时,它显示 HTTP错误:400错误请求:格式错误的主机头'

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.

推荐答案

要在不使用请求模块的情况下完成此操作,请尝试以下操作:

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

这篇关于Node.js:通过unix套接字发送GET请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆