每当我尝试运行代码时都会收到此错误。我也尝试过 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
如果您找不到谁在使用该端口,只需更改您正在运行的端口即可。