const mongoose = require('mongoose');
const server = '127.0.0.1:27017'; // REPLACE WITH YOUR OWN SERVER
const database = 'test'; // REPLACE WITH YOUR OWN DB NAME
const connectDB = async () => {
try {
await mongoose.connect(`mongodb://${server}/${database}`, {
useNewUrlParser: true,
useUnifiedTopology: true,
useFindAndModify: false,
useCreateIndex: true
});
console.log('MongoDB connected!!');
} catch (err) {
console.log('Failed to connect to MongoDB', err);
}
};
connectDB();
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter