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

时间:2019-12-15 18:52:31

标签: node.js express

我已经多次启动服务器,例如4000 5000 6000,但是在端口抛出器上得到了相同的答案; //未处理的“错误”事件,我不知道为什么更改了很多次,我也用ctrl c杀死了我的服务器,然后重新启动,但收到了同样的错误错误:监听EADDRINUSE:地址已在使用::: 6000

const express = require('express');
const app = express(); 
const Joi = require('joi');
app.use(express.json());
const port = 6000;

const courses =[
    { id:1,
         name:'courses 1'},
    { id:2, name:'courses 2'},
    { id:1, name:'courses 3'},
];
app.get('/',(req,res)=>{
    res.send("hellow  request for this site has been successfully done");
});
app.get('/api/courses',(req,res)=>{
    res.send(courses);
});
app.get('/api/courses/:id',(req,res)=>{
    let course = courses.find(c=> c.id === parseInt (req.params.id));
    if (!course)res.status(404).send('<h1> 404 found</h1>')
    res.send(course);
});
app.put('/api/courses/:id',(req,res)=>{
    let course = courses.find(c=> c.id === parseInt (req.params.id));
    if (!course)res.status(404).send('<h1> 404 found</h1>')
    const {error} = validateCourse(req.body);
    if(error){
        res.status(400).send(error)
        return;
    } 

    course.name = req.body.name;
    res.send(course);


});

app.post('/api/courses',(req,res)=>{

    const {error} = validateCourse(req.body);
    if(error){
        res.status(400).send(error)
        return;
    } 
    const course ={
        id: courses.length + 1,
        name : req.body.name
    };
    courses.push(course);
    res.send(course);
});
app.listen(port,()=>{
    console.log(" kokab your server is runing " + port)

})
// validatecourse for repetation of course
validateCourse = (course) =>{
    const schema = {
        name: Joi.string().min(3).required()
    };
    return result = Joi.validate(course, schema); 
}
// terminal error
    C:\Users\DELL\Desktop\node> node app
events.js:174
      throw er; // Unhandled 'error' event
      ^

Error: listen EADDRINUSE: address already in use :::6000
    at Server.setupListenHandle [as _listen2] (net.js:1279:14)
    at listenInCluster (net.js:1327:12)
    at Server.listen (net.js:1414:7)
    at Function.listen (C:\Users\DELL\Desktop\node\node_modules\express\lib\application.js:618:24)
    at Object.<anonymous> (C:\Users\DELL\Desktop\node\app.js:53:5)
    at Module._compile (internal/modules/cjs/loader.js:778:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
    at Module.load (internal/modules/cjs/loader.js:653:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
    at Function.Module._load (internal/modules/cjs/loader.js:585:3)
Emitted 'error' event at:
    at emitErrorNT (net.js:1306:8)
    at process._tickCallback (internal/process/next_tick.js:63:19)
    at Function.Module.runMain (internal/modules/cjs/loader.js:834:11)
    at startup (internal/bootstrap/node.js:283:19)
    at bootstrapNodeJSCore (internal/bootstrap/node.js:622:3)

2 个答案:

答案 0 :(得分:0)

使用下方命令杀死该进程

lsof -i:<port_number>

kill -9 <PID>

答案 1 :(得分:0)

您可以通过以下方式进行

if (role != null)
            {
                user.Role = roles.FirstOrDefault(u => u.Id == int.Parse(user.Role)).Id.ToString();
            }

sudo kill $(lsof -t -i:PORT) 

sudo fuser -k -n tcp PORT 

请将 PORT 替换为所需的端口。