【发布时间】:2014-04-25 06:40:48
【问题描述】:
我有两个小的 systemd 套接字和服务文件:
套接字:
[Socket]
ListenStream=80
# also tried with Accept=no
[Install]
WantedBy=sockets.target
服务:
[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
环境:
PORT=80
我有一个很小的 Node.js 脚本:
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());
});
如果我启动它:
$ systemctl start node-script.socket
$ curl localhost
Curl 就坐在那里。如果我尝试将 netcat 数据直接插入套接字,也是一样。检查日志,我注意到服务器建立了连接,但从未收到任何数据或请求。
listening on provided descriptor { address: '::', family: 'IPv6', port: 80 }
new connection { address: '::1', family: 'IPv6', port: 80 }
数据去哪儿了?
【问题讨论】:
-
如果您从命令行启动应用程序,
/path/to/node /path/to/server.js是否按预期工作? -
是的,当然。从那以后,我一直在解决我的问题,但我不记得是如何解决的。