我收到此错误,错误:侦听 EADDRINUSE:地址已在使用 :::5002,抛出者;未处理的'error'事件



每当我尝试运行代码时,我都会得到此错误。我也尝试过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)'))

此错误提示端口已被使用。

表示您的服务器没有关闭,您尝试在另一个终端上运行另一个服务器。或者另一个程序正在使用端口。您可以试着知道哪个程序使用了端口:https://veerasundar.com/blog/how-to-check-which-application-is-using-which-port

如果您无法找到正在使用的端口,只需更改您正在运行的端口。

最新更新