【发布时间】:2021-05-07 02:44:07
【问题描述】:
我正在运行一个简单的快速 https 网络服务器,服务于我的本地网络。该服务器在 Windows 10 机器上运行,能够毫无问题地与我局域网上的所有设备建立连接。直到几分钟过去,服务器似乎变得空闲,停止响应任何设备发出的任何请求。在控制台中按回车键可解决此问题,但不是可取的。有没有办法让节点始终运行?遗憾的是,我无法切换到其他操作系统,如果这是某种 Windows 任务聚焦功能,那么我可能会不走运。
Server.js:
const express = require("express");
const https = require("https");
const path = require("path");
const fs = require("fs");
const ip = require("ip");
const app = express();
const port = 443;
//determines what folder houses js, css, html, etc files - console.log the ip of a connected client
app.use(express.static(__dirname + "/public/"), function (req, res, next) {
const ip = req.ip;
console.log("Now serving ip:", "\x1b[33m", ip, "\x1b[37m");
next();
});
//determines which file is the index
app.get("/", function (req, res) {
res.sendFile(path.join(__dirname + "/public/index.html"));
});
var sslServer = https.createServer(
{
key: fs.readFileSync(path.join(__dirname, "certificate", "key.pem")),
cert: fs.readFileSync(
path.join(__dirname, "certificate", "certificate.pem")
),
},
app
);
//determines which port server should listen on - console.log when server has successfully started
sslServer.listen(port, function () {
console.log(
"Server has successfully started, available on:",
"\x1b[33m",
ip.address(),
"\x1b[37m",
"listening on port:",
"\x1b[33m",
+port,
"\x1b[37m"
);
console.log("CTRL + C to exit server");
});
将提供任何需要的信息。
【问题讨论】:
-
你在用powershell吗?
-
节点没有“空闲”——这听起来像是操作系统问题
-
服务器不应该像其他人提到的那样闲置,对我来说听起来也是一个操作系统问题。您能否尝试使用任何流程管理服务,例如:npmjs.com/package/pm2-windows-service
标签: javascript node.js