Kill all processes on port 300
xxxxxxxxxx
kill -9 $(lsof -ti:3000)
xxxxxxxxxx
# Find the processes running on a specific port (e.g., port 8080)
sudo lsof -i :8080
# Kill all processes running on the port (replace <PID> with the actual process IDs)
sudo kill -9 <PID>
xxxxxxxxxx
// /src/app/lib/db.js
import mongoose from 'mongoose'
const MONGODB_URI = process.env.MONGO_URI
if (!MONGODB_URI) {
throw new Error(
'Please define the MONGODB_URI environment variable inside .env.local',
)
}
let cached = global.mongoose
if (!cached) {
cached = global.mongoose = { conn: null, promise: null }
}
async function dbConnect() {
if (cached.conn) {
return cached.conn
}
if (!cached.promise) {
const opts = {
bufferCommands: false,
}
cached.promise = mongoose.connect(MONGODB_URI, opts).then(mongoose => {
console.log('Db connected')
return mongoose
})
}
try {
cached.conn = await cached.promise
} catch (e) {
cached.promise = null
throw e
}
return cached.conn
}
export default dbConnect