最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Node.js: Send GET request via unix socket - Stack Overflow

programmeradmin2浏览0评论

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 municate 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.

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 municate 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.

Share Improve this question edited Dec 16, 2016 at 6:02 Bista 7,9033 gold badges29 silver badges57 bronze badges asked Dec 16, 2016 at 4:36 Nikita KodkaniNikita Kodkani 1651 gold badge2 silver badges11 bronze badges 2
  • nodejs/api/net.html#net_server_listen_path_backlog_callback – Adiii Commented Dec 16, 2016 at 6:15
  • see this also stackoverflow./questions/7045614/… – Adiii Commented Dec 16, 2016 at 6:16
Add a ment  | 

2 Answers 2

Reset to default 17

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

Consider the following example :

client side code : Also you can do socket connection in some code :

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

server side code in nodejs : For this install npm, then 1)npm init 2)npm install --save express 3)npm insatll --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);
  reponse.write(message);
  reponse.end();
});

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

You can hit this from browser a check

发布评论

评论列表(0)

  1. 暂无评论