节点,React,同时,EADDRINUSE ::: 8000错误

时间:2018-06-11 02:13:50

标签: node.js reactjs express server

我尝试在后端使用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);

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

3 个答案:

答案 0 :(得分:1)

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

 concurrently "yarn server" "yarn client"
  • yarn server运行nodemon index.js
  • yarn client运行yarn start,执行node index.js

答案 1 :(得分:1)

在您的package.json中,将客户端脚本更改为此"client": "yarn --cwd client start",它应该可以运行?

答案 2 :(得分:0)

我通过运行以下命令解决了这个问题:

killall node