In this blog, you will learn How to connect MongoDB with Express Js which is a Node Js framework to make REST APIs.
1. Installing Mongoose
To install mongoose use this command "npm i mongoose" and import it in the index.js file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const express = require("express"); | |
const app = express(); | |
const mongoose = require("mongoose"); | |
mongoose | |
.connect( | |
"mongodb+srv://jatinhemnani01:8889994742@cluster0.yzqnh.mongodb.net/ecommerce?retryWrites=true&w=majority", | |
{ | |
useUnifiedTopology: true, | |
useNewUrlParser: true, | |
useCreateIndex: true, | |
} | |
) | |
.then(() => { | |
console.log("Connected To Database"); | |
}) | |
.catch((e) => { | |
console.log(e); | |
}); |
After installing and importing mongoose we will now connect to the Database.
The above code will connect MongoDB and express and now you can use the database. After running this file using the "node index" command it will show the message that the database is connected.
Result
If you liked this blog and want video tutorials do Subscribe to our YouTube channel here.
0 Comments
Don't Spam In Comments