Skip to content

Commit

Permalink
UPDATE: updating the way server retrieves 'env' variables.
Browse files Browse the repository at this point in the history
  • Loading branch information
AhmedMaherElSaeidi committed Jul 31, 2024
1 parent cfbf4fc commit 0c36fa7
Show file tree
Hide file tree
Showing 10 changed files with 45 additions and 36 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# Ignore all
.env
public/*
node_modules

Expand Down
14 changes: 8 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"
12 changes: 12 additions & 0 deletions config/config.js
Original file line number Diff line number Diff line change
@@ -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,
};
17 changes: 0 additions & 17 deletions config/database.config.json

This file was deleted.

13 changes: 6 additions & 7 deletions database.js
Original file line number Diff line number Diff line change
@@ -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
Expand Down
5 changes: 3 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
@@ -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();

Expand Down Expand Up @@ -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}`);
Expand Down
12 changes: 12 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
3 changes: 0 additions & 3 deletions routes/products.js
Original file line number Diff line number Diff line change
Expand Up @@ -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." });
Expand Down
3 changes: 2 additions & 1 deletion services/JWT.js
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down

0 comments on commit 0c36fa7

Please sign in to comment.