Secure Your Personal Data with DOTENV
Secure Your Personal Data with DOTENV
▶️ Setup .env file
- First install dotenv extension in vscode
- Now go to your main directory and open terminal and type
npm i dotenv - Now got to
app.jsand add this code at the top of the file
require('dotenv').config()- Now create a file named
.envin your main directory so typeecho > .envin your terminal and press enter - Now run the application again by pressing
npm run devand you will see that the application is running fine. - Now go to your
.envfile and add your MONGODB_URL
MONGODB_URL = "YOUR_MONGODB_URL"- Now go to connect.js and remove the
uri = "YOUR_MONGODB_URL"and as a parameter pass the uri
connect.js
const mongoose = require('mongoose');
const connectDB = (uri) => {
// console.log("Connecting to DB");
return mongoose.connect(uri,
{
useNewUrlParser: true,
useUnifiedTopology: true
});
};
module.exports = connectDB;- Now go to
app.jsand inside await connectDB() pass the uri
app.js
await connectDB(process.env.MONGODB_URL);