Skip to content

Commit

Permalink
fix: mongodb connection
Browse files Browse the repository at this point in the history
  • Loading branch information
Manthan Ankolekar committed Apr 2, 2023
1 parent f70ca1a commit c910a17
Showing 1 changed file with 11 additions and 14 deletions.
25 changes: 11 additions & 14 deletions backend/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,23 @@ const PORT = process.env.PORT || 3000;
const app = express();
const cors = require("cors");
const bodyParser = require("body-parser");
const { MongoClient } = require('mongodb');
const mongoose = require('mongoose');

require("dotenv").config();

const dbUser = process.env.MONGODB_USER;
const dbPassword = process.env.MONGODB_PASSWORD;

//method (secured)
const uri = `mongodb+srv://${dbUser}:${dbPassword}@cluster0.re3ha3x.mongodb.net/backend`;

const client = new MongoClient(uri, { useNewUrlParser: true, useUnifiedTopology: true });
const connect = async () => {
try {
await client.connect();
console.log('Connected successfully to MongoDB');
} catch (err) {
console.error(err);
}
}
connect();
mongoose
.connect(
`mongodb+srv://${dbUser}:${dbPassword}@cluster0.re3ha3x.mongodb.net/full-stack-portfolio`
)
.then(() => {
console.log("Connected to MongoDB database!");
})
.catch(() => {
console.log("Connection failed!");
});

app.use(
cors({
Expand Down

0 comments on commit c910a17

Please sign in to comment.