开发者

Can node.js listen on UNIX socket?

开发者 https://www.devze.com 2023-03-27 15:01 出处:网络
Can node.js listen on UNIX socket? I did not find any documentation regarding this. I only saw the possibility of listeni开发者_开发知识库ng on a dedicated port.To listen for incoming connections in n

Can node.js listen on UNIX socket? I did not find any documentation regarding this. I only saw the possibility of listeni开发者_开发知识库ng on a dedicated port.


To listen for incoming connections in node.js you want to use the net.server class.

The standard way of creating an instance of this class is with the net.createServer(...) function. Once you have an instance of this class you use the server.listen(...) function to tell the server where to actually listen.

If the first argument to listen is a number then nodejs will listen on a TCP/IP socket with that port number. However, if the first argument to listen is a string, then the server object will listen on a Unix socket at that path.

var net = require('net');

// This server listens on a Unix socket at /var/run/mysocket
var unixServer = net.createServer(function(client) {
    // Do something with the client connection
});
unixServer.listen('/var/run/mysocket');

// This server listens on TCP/IP port 1234
var tcpServer = net.createServer(function(client) {
    // Do something with the client connection
});
tcpServer.listen(1234);


Yes. It's in the documentation.

https://nodejs.org/api/net.html#net_server_listen_path_backlog_callback

0

精彩评论

暂无评论...
验证码 换一张
取 消