From 2040e675b823d6eb5c8be67026b9a7fecc3b475d Mon Sep 17 00:00:00 2001 From: Ankush Date: Mon, 2 Oct 2023 20:32:36 +0530 Subject: [PATCH] used dotenv and moved the config.json data inside the .env file and created a example.env text --- example.env.txt | 3 +++ package.json | 2 ++ src/app.js | 6 ++++-- 3 files changed, 9 insertions(+), 2 deletions(-) create mode 100644 example.env.txt diff --git a/example.env.txt b/example.env.txt new file mode 100644 index 0000000..60dc7ce --- /dev/null +++ b/example.env.txt @@ -0,0 +1,3 @@ +MONGODB_URL = mongodb://localhost:27017/course +SECRET_KEY = YOUR_SECRET_KEY_HERE +PORT = 3000 \ No newline at end of file diff --git a/package.json b/package.json index d86bdf6..12957f8 100644 --- a/package.json +++ b/package.json @@ -4,6 +4,7 @@ "connect-flash": "^0.1.1", "cookie-parser": "^1.4.6", "csurf": "^1.11.0", + "dotenv": "^16.3.1", "ejs": "^3.1.9", "express": "^4.18.2", "express-mongo-sanitize": "^2.2.0", @@ -21,6 +22,7 @@ "version": "0.0.1", "main": "src/app.js", "scripts": { + "start": "node src/app.js", "test": "echo \"Error: no test specified\" && exit 1" }, "repository": { diff --git a/src/app.js b/src/app.js index 641fbb5..83b1486 100644 --- a/src/app.js +++ b/src/app.js @@ -11,6 +11,8 @@ const rateLimit = require("express-rate-limit"); const csrf = require("csurf"); const cookieParser = require("cookie-parser"); const mongoSanitize = require("express-mongo-sanitize"); +const dotenv = require("dotenv"); +dotenv.config(); const courseModel = require("./db/courseDB"); @@ -37,7 +39,7 @@ const addCSRF = require("./middlewares/addCSRF"); // Connect to MongoDB using the configuration mongoose - .connect(config.mongodb_uri, { + .connect(process.env.MONGODB_URL, { useNewUrlParser: true, useUnifiedTopology: true, }) @@ -84,7 +86,7 @@ app.use(cookieParser()); //app.use(csrf()); //app.use(addCSRF) app.use( - session({ secret: config.secret_key, resave: false, saveUninitialized: true }) + session({ secret: process.env.SECRET_KEY, resave: false, saveUninitialized: true }) ); app.use(flash()); app.use(passport.initialize());