xxxxxxxxxx
### For Linux/Mac OS search (sudo) run this in the terminal:
$ lsof -i tcp:3000
$ kill -9 PID
### On Windows:
netstat -ano | findstr :3000
tskill typeyourPIDhere
### change tskill for taskkill in git bash
xxxxxxxxxx
# Find the process ID associated with port 3000
sudo lsof -i :3000
# The above command will give an output similar to:
# COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
# node 12345 your_username xxu IPv4 xxxxx 0t0 TCP *:3000 (LISTEN)
# Kill the process using the process ID obtained from the previous command
sudo kill 12345
xxxxxxxxxx
# If node and npm (v5.2+) are installed:
npx kill-port <PORT>
# e.g. npx kill-port 5000
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