Question

I've got two little systemd socket and service files:

Socket:

[Socket]
ListenStream=80
# also tried with Accept=no

[Install]
WantedBy=sockets.target

Service:

[Unit]
Description=Test Webserver

[Service]
EnvironmentFile=/path/to/node-script.env
ExecStart=/path/to/node /path/to/server.js
Restart=always
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=node-script
Type=simple
User=test-user
Group=test-user

[Install]
WantedBy=sockets.target

Env:

PORT=80

I've got a tiny Node.js script:

var http = require('http');
var server = http.createServer();
server.on('connection', function(conn) {
  // socket connection
  console.log('new connection', conn.address());
  conn.on('data', function(data) {
    console.log('more data', data.length);
  });
}).on('request', function(req, res) {
  // a request!
  console.log('new request', req.headers);
}).listen({fd: 3}, function() {
  console.log('listening on provided descriptor', server.address());
});

If I start it up:

$ systemctl start node-script.socket
$ curl localhost

Curl just sits there. Same thing if I try to directly netcat data into the socket. Checking the logs, I note that the server got a connection, but never got any data or a request.

listening on provided descriptor { address: '::', family: 'IPv6', port: 80 }
new connection { address: '::1', family: 'IPv6', port: 80 }

Where's the data going?

Was it helpful?

Solution

In case anybody else encounters this problem: In the systemd [Service] section, you need to add this line:

NonBlocking=True

Though this should only be necessary for old releases; node.js was fixed to set the non-blocking flag itself somewhere in the 0.12 series.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top