Tutorial
Secure with DOTENV

Secure Your Personal Data with DOTENV

Secure Your Personal Data with DOTENV

▶️ Setup .env file

  1. First install dotenv extension in vscode
  2. Now go to your main directory and open terminal and type npm i dotenv
  3. Now got to app.js and add this code at the top of the file
require('dotenv').config()
  1. Now create a file named .env in your main directory so type echo > .env in your terminal and press enter
  2. Now run the application again by pressing npm run dev and you will see that the application is running fine.
  3. Now go to your .env file and add your MONGODB_URL
MONGODB_URL = "YOUR_MONGODB_URL"
  1. 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;
  1. Now go to app.js and inside await connectDB() pass the uri
app.js
 
await connectDB(process.env.MONGODB_URL);