【问题标题】:Express server not exiting process and re-freeing portExpress 服务器未退出进程并重新释放端口
【发布时间】:2021-08-07 07:57:46
【问题描述】:

我在节点应用程序中有一个快速服务器。我需要能够使用调试器运行此节点应用程序。我还需要能够轻松地停止和重新启动服务器,而无需绑定它运行的端口。我需要利用关机进行一些清理,我这样做是这样的:

const server = app.listen(port, () => {
    console.log("Listening on port whatever");
});

process.on('SIGINT', function () {
    server.close(() => {
        console.log(chalk.blue('Shutting down server'));
        // some cleanup code
        process.exit();
    });
});

我的启动脚本使用 nodemon 在保存时重新启动。以下是我的 package.json 中与此相关的相关部分:

{
    "scripts": {
        "start:debug": "node --inspect -r tsconfig-paths/register -r ts-node/register ./src/server.ts",
        "start": "nodemon",
    },
    "nodemonConfig": {
        "ignore": [...],
        "watch": ["src"],
        "exec": "npm run start:debug",
        "ext": "ts"
    },
    "configurations": [
        {
            "type": "node",
            "request": "attach",
            "name": "Node: Nodemon",
            "processId": "${command:PickProcess}",
            "restart": true,
            "protocol": "inspector"
        }
    ]
}

这似乎有效。它正常启动,可通过节点调试器访问,并在使用 nodemon 保存时重新启动。当我点击ctrl + c 时,服务器关闭并记录"Shutting down server"。但是,即使在记录该 get 之后,节点也会记录 Waiting for the debugger to disconnect...。当我再次点击ctrl + c 并重新运行npm start 时,我得到一个提示Error: listen EADDRINUSE: address already in use :::PORT# 的崩溃。

这里出了什么问题?我的服务器似乎正在正常关闭,但调试器似乎没有成功关闭,并且有一个节点进程仍在占用该端口。干净地启动和停止我的服务器并调试问题变得越来越困难。

【问题讨论】:

  • 您是否停止或分离了您的调试客户端?听起来它可能仍在处理某些事情,因此调试服务器尚未关闭?
  • 我不知道该怎么做,无论是手动还是编程。对我有什么想法吗?
  • 你用什么做调试器?
  • 我正在使用 chrome 的节点调试器
  • 然后,关闭运行调试器的选项卡。这将断开调试器。

标签: javascript node.js express process nodemon


【解决方案1】:

要使调试器断开连接,您需要关闭其中包含调试器连接的 Chrome 选项卡。这将允许您的应用程序的前一个实例完全退出。

【讨论】:

    猜你喜欢
    • 2010-10-07
    • 1970-01-01
    • 1970-01-01
    • 2012-09-04
    • 2018-10-22
    • 1970-01-01
    • 1970-01-01
    • 2020-11-16
    • 2021-06-05
    相关资源
    最近更新 更多