From 0c36fa7ad1f94b3d175fabdcd5db3af3983390eb Mon Sep 17 00:00:00 2001 From: Ahmed Maher Date: Wed, 31 Jul 2024 22:57:16 +0300 Subject: [PATCH] UPDATE: updating the way server retrieves 'env' variables. --- .gitignore | 1 + README.md | 14 ++++++++------ config/config.js | 12 ++++++++++++ config/database.config.json | 17 ----------------- database.js | 13 ++++++------- index.js | 5 +++-- package-lock.json | 12 ++++++++++++ package.json | 1 + routes/products.js | 3 --- services/JWT.js | 3 ++- 10 files changed, 45 insertions(+), 36 deletions(-) create mode 100644 config/config.js delete mode 100644 config/database.config.json diff --git a/.gitignore b/.gitignore index a36b22d..5785f09 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ # Ignore all +.env public/* node_modules diff --git a/README.md b/README.md index 4b6bdd1..b2c6c6b 100644 --- a/README.md +++ b/README.md @@ -93,9 +93,11 @@ You can find the frontend implementation [here](https://github.com/AhmedMaherElS Ensure you have the following variables set in your `.env` file: ```plaintext -DB_HOST=localhost -DB_USER=root -DB_PASSWORD=yourpassword -DB_NAME=ecommerce -JWT_SECRET=your_jwt_secret -PORT=5000 +HOST="localhost" +PORT=3600 +DB_NAME="electrifydb" +DB_DIALECT= "mysql" +DB_USERNAME="root" +DB_PASSWORD="" +DB_PORT=3306 +JWT_KEY="5BD24DCB1483578373DD86A7AD35F" diff --git a/config/config.js b/config/config.js new file mode 100644 index 0000000..20d4fe4 --- /dev/null +++ b/config/config.js @@ -0,0 +1,12 @@ +require("dotenv").config(); + +module.exports = { + HOST: process.env.HOST, + PORT: process.env.PORT, + DB_NAME: process.env.DB_NAME, + DB_PORT: process.env.DB_PORT, + DB_DIALECT: process.env.DB_DIALECT, + DB_USERNAME: process.env.DB_USERNAME, + DB_PASSWORD: process.env.DB_PASSWORD, + JWT_KEY: process.env.JWT_KEY, +}; diff --git a/config/database.config.json b/config/database.config.json deleted file mode 100644 index 0610ee2..0000000 --- a/config/database.config.json +++ /dev/null @@ -1,17 +0,0 @@ -{ - "development": { - "database": "electrifydb", - "host": "localhost", - "port":3306, - "username": "root", - "password": "", - "dialect": "mysql" - }, - "production": { - "username": null, - "password": null, - "database": null, - "host": null, - "dialect": null - } -} diff --git a/database.js b/database.js index 1c87d78..82fd5c3 100644 --- a/database.js +++ b/database.js @@ -1,14 +1,13 @@ const Sequelize = require("sequelize"); -const config = require("./config/database.config.json"); +const config = require("./config/config"); // DB Connection Variables -const env = process.env.NODE_ENV || "development"; -const { host, port, dialect, username, password, database } = config[env]; +const { HOST, DB_USERNAME, DB_PASSWORD, DB_NAME, DB_DIALECT, DB_PORT } = config; -const sequelize = new Sequelize(database, username, password, { - host, - port, - dialect, +const sequelize = new Sequelize(DB_NAME, DB_USERNAME, DB_PASSWORD, { + HOST, + DB_PORT, + dialect: DB_DIALECT, }); // connect to mysql db diff --git a/index.js b/index.js index 9b4d68b..e339795 100644 --- a/index.js +++ b/index.js @@ -1,7 +1,8 @@ const { authorized } = require("./middlewares/authorization"); const bodyParser = require("body-parser"); -const cors = require("cors"); +const config = require("./config/config"); const morgan = require("morgan"); +const cors = require("cors"); const express = require("express"); const app = express(); @@ -30,7 +31,7 @@ app.use("/api/cartItems", authorized, cartItems_routes); app.use("/api/categories", categories_routes); // Server listening -const PORT = process.env.PORT || 3600; +const PORT = config.PORT; app.listen(PORT, () => { console.log("===================================="); console.log(`Server is listening... PORT:${PORT}`); diff --git a/package-lock.json b/package-lock.json index 8a43cf5..d7cf0e1 100644 --- a/package-lock.json +++ b/package-lock.json @@ -12,6 +12,7 @@ "bcrypt": "^5.1.1", "body-parser": "^1.20.2", "cors": "^2.8.5", + "dotenv": "^16.4.5", "express": "^4.19.2", "joi": "^17.13.3", "jsonwebtoken": "^9.0.2", @@ -539,6 +540,17 @@ "node": ">=8" } }, + "node_modules/dotenv": { + "version": "16.4.5", + "resolved": "https://registry.npmjs.org/dotenv/-/dotenv-16.4.5.tgz", + "integrity": "sha512-ZmdL2rui+eB2YwhsWzjInR8LldtZHGDoQ1ugH85ppHKwpUHL7j7rN0Ti9NCnGiQbhaZ11FpR+7ao1dNsmduNUg==", + "engines": { + "node": ">=12" + }, + "funding": { + "url": "https://dotenvx.com" + } + }, "node_modules/dottie": { "version": "2.0.6", "resolved": "https://registry.npmjs.org/dottie/-/dottie-2.0.6.tgz", diff --git a/package.json b/package.json index 416f21f..2c14fc5 100644 --- a/package.json +++ b/package.json @@ -15,6 +15,7 @@ "bcrypt": "^5.1.1", "body-parser": "^1.20.2", "cors": "^2.8.5", + "dotenv": "^16.4.5", "express": "^4.19.2", "joi": "^17.13.3", "jsonwebtoken": "^9.0.2", diff --git a/routes/products.js b/routes/products.js index 1b41e7b..ec8e2b7 100644 --- a/routes/products.js +++ b/routes/products.js @@ -102,9 +102,6 @@ router.put("/:id", authorized, upload.single("image"), async (req, res) => { const imagePath = path.join(__dirname, "../public", oldProduct.image); const deleted = await File.deleteFile(imagePath); if (!deleted) { - console.log('===================================='); - console.log("IM HERE"); - console.log('===================================='); return res .status(400) .json({ message: "Error encountered while removing image." }); diff --git a/services/JWT.js b/services/JWT.js index c05a1d6..f07b908 100644 --- a/services/JWT.js +++ b/services/JWT.js @@ -1,7 +1,8 @@ const jwt = require("jsonwebtoken"); +const config = require("../config/config"); class JWT { - #key = "5BD24DCB1483578373DD86A7AD35F"; + #key = config.JWT_KEY; #ms = "3600s"; getAuthToken(user) {