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.js
and add this code at the top of the file
require('dotenv').config()
- Now create a file named
.env
in your main directory so typeecho > .env
in your terminal and press enter - Now run the application again by pressing
npm run dev
and you will see that the application is running fine. - Now go to your
.env
file 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.js
and inside await connectDB() pass the uri
app.js
await connectDB(process.env.MONGODB_URL);