nodejs and xinetd redux

167 views
Skip to first unread message

tedx

unread,
Sep 11, 2013, 12:19:05 PM9/11/13
to nod...@googlegroups.com
On a much older version of node I'd come up with a solution but that no longer works. For those who don't know xinetd listens for incoming requests over a network and launches the appropriate service for that request. The service communicates with its client by reading from stdin and writing to stdout. I tried:

// Configure our HTTP server to respond with Hello World to all requests.
var server = http.createServer(function (request, response) {
  response.writeHead(200, {"Content-Type": "text/plain"});
  response.end("Hello World\n");
});
// Listen on stdin
server.listen({fd: 0});

but this fails because the connection has already been established by xinetd. Is there a way to set the server connection to stdin and cause the callback to be executed without listening?

Ted

tedx

unread,
Sep 11, 2013, 3:32:09 PM9/11/13
to nod...@googlegroups.com
I can emit a 'connection' event but I'm not sure how to setup its argument. I tried:

var s = new net.Socket({fd: 0});
server.emit('connection', s);

but this just hangs maybe because the response needs to be written to stdout but the socket knows nothing about this. Is there a way to create a socket specifying 2 file descriptors? Or maybe there's another way to create a Duplex object from stdin and stdout that I can pass with the connection event?

tedx

unread,
Sep 11, 2013, 5:39:09 PM9/11/13
to nod...@googlegroups.com
------------------- /etc/xinetd.d/http ---------------------------------------------------------------------------------------
service http
{
type = UNLISTED
disable = no
protocol = tcp
server = /usr/bin/node
server_args = <path to http.js>/http.js
wait = no
instances = 20
socket_type = stream
port = 8000
user = root
}

--------------------------------------------- http.js -------------------------------------------
// Load the http module to create an http server.
var http = require('http');
var net = require('net');

// Configure our HTTP server to respond with Hello World to all requests.
var server = http.createServer(function (request, response) {
response.writeHead(200, {"Content-Type": "text/plain"});
response.end("Hello World\n");
process.stdout.pipe(response);
});

var sin = new net.Socket({fd: 0});

server.emit('connection', sin);

Artem Zhdanov

unread,
Jun 23, 2017, 4:02:09 AM6/23/17
to nodejs
Thank you so much!  Your solution works, and now I can use  nodejs and  xinetd !!!

Artem
Reply all
Reply to author
Forward
0 new messages