gpt4 book ai didi

node.js - 错误: listen EADDRINUSE after restart

转载 作者:太空宇宙 更新时间:2023-11-04 00:12:16 25 4
gpt4 key购买 nike

我有一个在本地端口 3000 上运行的 Node 应用程序。最近开始出现此错误:

➜  web-frontend git:(feature/WEB-6880__CheckoutBackButton) yarn start
yarn run v1.2.1
events.js:160
throw er; // Unhandled 'error' event
^

Error: listen EADDRINUSE :::3000
at Object.exports._errnoException (util.js:1022:11)
at exports._exceptionWithHostPort (util.js:1045:20)
at Server._listen2 (net.js:1259:14)
at listen (net.js:1295:10)
at Server.listen (net.js:1391:5)
at EventEmitter.listen (/Users/durham/Sites/web-frontend/node_modules/express/lib/application.js:618:24)
at Object.<anonymous> (/Users/durham/Sites/web-frontend/server/index.js:79:5)
at Module._compile (module.js:570:32)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
at Module.runMain (module.js:604:10)
at run (bootstrap_node.js:394:7)
at startup (bootstrap_node.js:149:9)
at bootstrap_node.js:509:3
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
➜ web-frontend git:(feature/WEB-6880__CheckoutBackButton)

我会终止使用该端口的任何进程,它会重新启动:

➜  ~ sudo lsof -i tcp:3000
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
httpd 1926 root 6u IPv6 0x99102dc7c7853bbb 0t0 TCP *:hbci (LISTEN)
httpd 1931 _www 6u IPv6 0x99102dc7c7853bbb 0t0 TCP *:hbci (LISTEN)
➜ ~ sudo kill -9 1926
➜ ~ sudo kill -9 1931
kill: 1931: No such process
➜ ~ z front
➜ web-frontend git:(feature/WEB-6880__CheckoutBackButton) yarn start
yarn run v1.2.1
$ export $(cat internals/env/dev); NODE_ENV=development node server
events.js:160
throw er; // Unhandled 'error' event
^

Error: listen EADDRINUSE :::3000
at Object.exports._errnoException (util.js:1022:11)
at exports._exceptionWithHostPort (util.js:1045:20)
at Server._listen2 (net.js:1259:14)
at listen (net.js:1295:10)
at Server.listen (net.js:1391:5)
at EventEmitter.listen (/Users/hco/Sites/web-frontend/node_modules/express/lib/application.js:618:24)
at Object.<anonymous> (/Users/hco/Sites/web-frontend/server/index.js:79:5)
at Module._compile (module.js:570:32)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
at Module.runMain (module.js:604:10)
at run (bootstrap_node.js:394:7)
at startup (bootstrap_node.js:149:9)
at bootstrap_node.js:509:3
error Command failed with exit code 1.

我重新启动计算机并尝试启动我的应用程序,但出现相同的错误。

这里可能存在什么问题?这是新的,我想不出我所做的任何更改可能导致它。

根据要求表达应用代码:

const express = require('express');
const logger = require('./logger');

const { sources } = require('../internals/config');
const argv = require('minimist')(process.argv.slice(2));
const setup = require('./middlewares/frontendMiddleware');
const path = require('path');
const resolve = require('path').resolve;
const fs = require('fs');
const morgan = require('morgan');

const app = express();

const DEFAULT_PORT = 3000;

/**
* Set up Morgan for logging errors to the server.
* All errors are logged to the `access.log` file in the directory the
* site is run from.
*/
const errorLog = process.env.NODE_ENV === 'production'
? '/var/log/web2/web2.log'
: path.join(process.cwd(), '../access.log');

// create a write stream (in append mode)
const accessLogStream = fs.createWriteStream(errorLog, { flags: 'a' });

// setup the logger
app.use(morgan('combined', {
skip: (req, res) => res.statusCode < 400, // eslint-disable-line no-magic-numbers
stream: accessLogStream
}));

// remove trailing slashes
app.use((req, res, next) => {
if (req.path.length > 1 && /\/$/.test(req.path)) {
const query = req.url.slice(req.path.length);
res.redirect(301, req.path.slice(0, -1) + query);
} else {
next();
}
});

// Proxy package so we can manage API requests with Node instead of the client.
const proxy = require('express-http-proxy');

/**
* Incorporate API middleware with proxy
* The proxy helps work around the CORS issue with standard Ajax requests
* on the client. There are further methods that can be taken advantage of with
* the express-http-proxy library. Take a look here:
* https://github.com/villadora/express-http-proxy
*
*
* Proxies requests for the H&Co API.
* Example URL to test: /api/v1/product_lines?sort=name
* API documentation: https://swagger.typography.com/api2/
*/
app.use('/api', proxy(sources.apiUrl, {
proxyReqOptDecorator: (proxyReq) => {
if (proxyReq.headers['authorization-api']) {
proxyReq.headers['Authorization'] = proxyReq.headers['authorization-api']; // eslint-disable-line
}
return proxyReq;
}
}));

// In production we need to pass these values in instead of relying on webpack
setup(app, {
outputPath: resolve(process.cwd(), 'build'),
publicPath: '/'
});

// get the intended port number, use port 3000 if not provided
const port = argv.port || process.env.PORT || DEFAULT_PORT;

// Start your app.
/* eslint consistent-return: 0 */
app.listen(port, (err) => {
if (err) {
return logger.error(err.message);
}
logger.appStarted(port, null, sources.apiUrl);
});

最佳答案

不要使用“app.listen”,而是像这样启动服务器:

const server = require("http").createServer(app);
server.listen(port, () => console.log("server started"));

然后您就可以在应用退出时关闭服务器并释放端口

function exit(message) {
if (message) console.log(message);
server.close();
process.exit();
}
process.on("exit", exit);
process.on("SIGINT", exit);
process.on("uncaughtException", exit);

关于node.js - 错误: listen EADDRINUSE after restart,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48910855/

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