【问题标题】:Nodejs web server goes idle when not in direct useNodejs Web 服务器在不直接使用时处于空闲状态
【发布时间】: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");
});

将提供任何需要的信息。

【问题讨论】:

标签: javascript node.js


【解决方案1】:

这是 Powershell 中的一个问题。它会在单击命令窗口时停止执行,直到用户按 Enter 键。打开 Power Shell 属性,取消勾选“快速编辑模式”和“插入模式”。

【讨论】:

    猜你喜欢
    • 2016-07-20
    • 1970-01-01
    • 2022-10-01
    • 1970-01-01
    • 2017-08-27
    • 2016-01-25
    • 1970-01-01
    • 1970-01-01
    • 2022-08-05
    相关资源
    最近更新 更多