我正在尝试将Express服务器作为systemd服务运行。但是,设置完成后,对它的请求将等待一分钟,然后由于504网关超时而失败。
何时: sudo systemctl停止nodeserver.service
响应立即是502错误的网关。当我在没有systemd的情况下执行node server.js
时,所有响应代码均为200。
这是我的systemd文件:
[Unit]
Description=Express server
[Service]
Type=simple
ExecStart=/usr/bin/node /opt/nodeserver/server.js
#WorkingDirectory=/opt/nodeserver
Restart=always
# Restart service after 10 seconds if node service crashes
RestartSec=10
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=nodejs-example
[Install]
WantedBy=multi-user.target
这是运行时的状态
nodeserver.service - Express server
Loaded: loaded (/etc/systemd/system/nodeserver.service; enabled; vendor preset: enabled)
Active: active (running) since Mon 2019-03-11 11:27:31 UTC; 39s ago
Main PID: 24419 (node)
Tasks: 6 (limit: 1152)
CGroup: /system.slice/nodeserver.service
└─24419 /usr/bin/node /opt/nodeserver/server.js
这是我的express.js服务器:
const express = require("express")
const cors = require("cors")
const app = express();
const PORT = 1234;
const spawn = require("child_process").spawn;
app.use(cors())
app.listen(PORT, function () {
console.log(`listening on port:${PORT}...`)
})
app.get("/api/play/:choice", function (req, res) {
pythonProcess = spawn('python', ["./script.py", req.params.choice]);
pythonProcess.stdout.on('data', function (data) {
let string = data.toString('utf-8')
const json = JSON.parse('{' + string.replace(/(\w+):(\w+)/g, `"$1":"$2"`) + '}')
res.status(200).send(json)
})
})
express.js服务器在lcoalhost:1234 / {params}上可用,并且服务也应该如此