gpt4 book ai didi

javascript - Nodemon:错误:监听 EADDRINUSE:地址已在使用中:::5000

转载 作者:行者123 更新时间:2023-12-02 21:07:49 25 4
gpt4 key购买 nike

我正在创建一个项目并使用nodejs,express作为后端。一切正常,但当我在文件中进行任何更改时,nodemon 由于以下错误而无法重新启动服务器:

错误:监听 EADDRINUSE:地址已在使用中:::5000

index.js:

const express = require("express");
const morgan = require("morgan");
const mongoose = require("mongoose");
const cookieParser = require("cookie-parser");
const session = require("express-session");
const FileStore = require("session-file-store")(session);
const dotenv = require("dotenv");
var passport = require("passport");

dotenv.config();

const PORT = process.env.PORT || 5000;

const app = express();

.....

app.listen(PORT, () => console.log(`Server listening on port ${PORT}!`));

package.json

{
"name": "chat-app-backend",
"version": "0.0.0",
"private": true,
"scripts": {
"start": "node index.js",
"dev": "nodemon --ignore 'sessions/' index.js"
},
"dependencies": {
"bcryptjs": "^2.4.3",
"cookie-parser": "^1.4.5",
"cors": "^2.8.5",
"debug": "~2.6.9",
"dotenv": "^8.2.0",
"express": "~4.16.0",
"express-session": "^1.17.0",
"http-errors": "~1.6.2",
"jade": "~1.11.0",
"jsonwebtoken": "^8.5.1",
"mongoose": "^5.9.4",
"morgan": "~1.9.0",
"passport": "^0.4.1",
"passport-jwt": "^4.0.0",
"passport-local": "^1.0.0",
"passport-local-mongoose": "^6.0.1",
"session-file-store": "^1.4.0",
"uuid": "^7.0.2"
},
"devDependencies": {
"nodemon": "^2.0.2"
}
}

我每次都必须从终端显式终止服务器,这不是最佳解决方案。我尝试了几件事,但没有一个起作用。甚至在nodemon GitHub问题页面中发现了一些问题,但我也找不到任何东西。

我还添加了 lsof -i:5000 的输出,即使服务器我正在关闭服务器 - *node 31625 rishav 20u IPv6 5300049 0t0 TCP: 5000(听)

最佳答案

--delay 帮助我解决了这两个问题

  • 自动重启

  • 使用 ctrl-c 停止

    nodemon --delay 500ms app.js

我还补充道:

process.once('SIGUSR2', function () {
process.kill(process.pid, 'SIGUSR2');
});

process.on('SIGINT', function () {
// this is only called on ctrl+c, not restart
process.kill(process.pid, 'SIGINT');
});

关于javascript - Nodemon:错误:监听 EADDRINUSE:地址已在使用中:::5000,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61181302/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com