const mongoose = require('mongoose');
mongoose.connect('mongodb://localhost:27017/test', {useNewUrlParser: true, useUnifiedTopology: true});
- In your entry file
mongoose
.connect("mongodb://localhost/vidly")
.then(() => console.log("Connected to MongoDB..."))
.catch((err) => console.log("Cloud not connect to MongoDB..."));
mongoose.connect('mongodb://username:password@host:port/database?options...');
//connect with mongodb
mongoose.connect('mongodb://localhost:27017/your_db_name', {useNewUrlParser: true});
//you can also specify with user and pass
mongoose.connect('mongodb://username:password@host:port/database?options...', {useNewUrlParser: true});
//or goto docs https://mongoosejs.com/docs/connections.html
//config/db.js
const mongoose = require('mongoose');
const connect = async () => {
try {
await mongoose.connect(process.env.MONGODB);
console.log('database connect');
} catch (error) {
console.log(error.message);
}
};
module.exports = connect;
//index.js
require('dotenv').config();
const connect = require('./config/db');
connect();
// Connect to the Database here
mongoose.connect("mongodb://localhost:27017/test", {
useNewUrlParser: true,
useUnifiedTopology: true,
}, (err) => {
if (err) return console.log(err);
app.listen(3000, () => {
console.log("MongoDB Server listening on 3000");
});
});
const mongoose=require('mongoose');
const mongoURI="mongodb://localhost:27017/inotebook"
const connectToMongo=()=>
{
mongoose.connect(mongoURI,()=>
{
console.log("connect Successfully");
})
}
module.exports=connectToMongo;