错误:重启后侦听EADDRINUSE

时间:2018-02-21 16:29:44

标签: node.js express process port

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

➜  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);
});

1 个答案:

答案 0 :(得分:0)

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

<?php
$id = $_POST['id']; 

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

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