Search code examples
node.jssocketshttpraspberry-piunix-socket

Node.js HTTP won't make connection with Unix Socket on Raspberry Pi


So I have my Raspberry Pi currently running a unix socket with the library and if I make a simple get request in my browser like 10.0.0.200:2500 I get the response message I want (just a string of text). I also get the same result using PostMan so I KNOW the socket is working find.

The issue is when I am using Node.js to make this same request with

var options = {
  host: '10.0.0.200',
  port: 2500,
  path: '/'
};

http.get(options, function(res) {
  console.log("Got response: " + res.statusCode);
}).on('error', function(e) {
  console.log("Got error: " + e.message);
});

I keep getting a { [Error: Parse Error] bytesParsed: 0, code: 'HPE_INVALID_CONSTANT' } message in node

My socket is also showing

GET / HTTP/1.1
Host: 10.0.0.200:2500
Connection: close

So Idk why the conneciton is closed and I even tried sending a header with connection : keep-alive and that didn't help either


Solution

  • Thanks to @roberklep

    I got it work using net.connect() and passing the options argument found here net.connect()

    also another good source for example of use here