1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
| const mongoose = require('mongoose');
const uri = 'mongodb://localhost:27017/mydb';
mongoose.connect(uri, {
useNewUrlParser: true,
useUnifiedTopology: true,
serverSelectionTimeoutMS: 5000,
socketTimeoutMS: 45000,
})
.then(() => console.log('MongoDB connected'))
.catch(err => console.error('Connection error:', err));
// イベントハンドリング
mongoose.connection.on('error', (err) => {
console.error('MongoDB error:', err);
});
mongoose.connection.on('disconnected', () => {
console.log('MongoDB disconnected');
});
|