我收到此错误,错误:监听 EADDRINUSE:地址已在使用 :::5002,抛出错误; // 未处理的“错误”事件

问题描述 投票:0回答:2

每当我尝试运行代码时都会收到此错误。我也尝试过 Ctrl+C 并尝试了不同的端口,但它不起作用。我还使用命令来终止所有已经运行的端口。 这是下面的代码。我使用了这个命令taskkill /im node.exe。我希望代码没有问题。它的技术问题。

  const express= require('express') 
  const bodyParser=require('body-parser') 
  const mysql=require('mysql')

  const app= express()
   const port=process.env.PORT || 5002


  app.use(bodyParser.urlencoded({extended:false}))

  app.use(bodyParser.json())

 //mysql
const pool=mysql.createPool({

connectionLimit:10,
host           :'localhost',
user           :'root',
password       :'',
database       :'nodejs_beers'

})

 //Get All beers
 app.get('',(req,res)=>{

    pool.getConnection((err,connection)=>{

        if(err) throw err
        console.log(`connected as id ${connection.threadID}`)

        connection.query('SELECT * from beers',(err,rows)=>{

            connection.release()

            if(!err){
                res.send(rows)
            }
            else{
                console.log(err)

            }
        })

    })


   })

   //delete A beer by id
   app.delete('/:id',(req,res)=>{

    pool.getConnection((err,connection)=>{

        if(err) throw err
        console.log(`connected as id ${connection.threadID}`)

        connection.query('DELETE from beers WHERE id = ?',[req.params.id],(err,rows)=>{

            connection.release()

            if(!err){
                res.send(`Beer with the id ${[req.params.id]} has been deleted`)
            }
            else{
                console.log(err)

            }
        })

     })


   })

  //Inser A beer 
  app.delete('',(req,res)=>{

    pool.getConnection((err,connection)=>{

        if(err) throw err
        console.log(`connected as id ${connection.threadID}`)
        const params=req.body

        connection.query('INSERT INTO beers SET = ?',params,[req.params.id],(err,rows)=>{

            connection.release()

            if(!err){
                res.send(`Beer with the id: ${params.id} has been added`)
            }
            else{
                console.log(err)

            }
        })

    })


   })



  //listen on environment port
  app.listen(port,()=>console.log('listen on port  $(port)'))
node.js port nodejs-server
2个回答
1
投票

此错误表明该端口已在使用中。

这意味着您的服务器未关闭,并且您尝试在另一个终端中运行另一个服务器。 或者另一个程序确实正在使用该端口。 您可以尝试以下方法来了解哪个程序使用端口:https://veerasundar.com/blog/how-to-check-which-application-is-using-which-port

如果您找不到谁在使用该端口,只需更改您正在运行的端口即可。


0
投票

我在处理全栈项目时遇到了这个错误:

  code: 'EADDRINUSE',
  errno: -4091,
  syscall: 'listen',
  address: '::',
  port: 5555 

然后我意识到在 mongoose.connect() 函数中编写这个函数。在导致该错误之前,请检查您的代码,不要像我一样失败:) enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.