代码之家  ›  专栏  ›  技术社区  ›  MMelvin0581

节点,react,with concurrent,eaddrinuse::8000错误

  •  0
  • MMelvin0581  · 技术社区  · 7 年前

    我想用npm软件包 concurrently 在后端有一个node/express服务器,并且运行 create-react-app 的服务器在前端。这里是 package.json 脚本:

    "scripts": {
    "start": "node index.js",
    "server": "nodemon index.js",
    "client": "yarn start --prefix client",
    "dev": "concurrently \"yarn server\" \"yarn client\""
    },
    

    当我执行时 yarn dev 我得到以下错误:

    $ concurrently "yarn server" "yarn client"
    $ nodemon index.js
    $ yarn start --prefix client
    [0] [nodemon] 1.17.5
    [0] [nodemon] to restart at any time, enter `rs`
    [0] [nodemon] watching: *.*
    [0] [nodemon] starting `node index.js`
    $ node index.js --prefix client
    [1] events.js:183
    [1]       throw er; // Unhandled 'error' event
    [1]       ^
    [1]
    [1] Error: listen EADDRINUSE :::8000
    [1]     at Object._errnoException (util.js:1022:11)
    [1]     at _exceptionWithHostPort (util.js:1044:20)
    [1]     at Server.setupListenHandle [as _listen2] (net.js:1367:14)
    [1]     at listenInCluster (net.js:1408:12)
    [1]     at Server.listen (net.js:1492:7)
    [1]     at Function.listen
    ...
    error Command failed with exit code 1.
    info Visit https://yarnpkg.com/en/docs/cli/run for documentation about 
    this command.
    error Command failed with exit code 1.
    info Visit https://yarnpkg.com/en/docs/cli/run for documentation about 
    this command.
    

    当我使用 lsof -i tcp:8000 它什么也不返回。我不知道还能做什么。感谢大家的帮助。

    以下是服务器代码:

    const express = require('express');
    const mongoose = require('mongoose');
    const keys = require('./config/keys');
    const cookieSession = require('cookie-session');
    const passport = require('passport');
    require('./models/User');
    require('./services/passport');
    
    mongoose.connect(keys.mongoURI);
    
    const app = express();
    
    // enable cookies
    app.use(
      cookieSession({
        // cookie good for 30 days
        maxAge: 30 * 24 * 60 * 60 * 1000,
        // authenticate cookie
        keys: [keys.cookieKey]
      })
    );
    // tell passport to use cookies
    app.use(passport.initialize());
    app.use(passport.session());
    
    // get the routes
    require('./routes/authRoutes')(app);
    
    const PORT = process.env.PORT || 5000;
    app.listen(PORT);
    

    无论我将端口更改为什么,它都会为该端口提供相同的错误。

    1 回复  |  直到 7 年前
        1
  •  1
  •   Marcos Casagrande    7 年前

    好吧,你执行了两次 index.js 启动服务器监听端口8000,当然会触发 Error: listen EADDRINUSE :::8000

     concurrently "yarn server" "yarn client"
    
    • yarn server nodemon index.js
    • yarn client yarn start 执行 node index.js
    推荐文章