Skip to content

Commit ed52290

Browse files
committed
dynamic env file
1 parent 12aa9f4 commit ed52290

File tree

13 files changed

+73
-37
lines changed

13 files changed

+73
-37
lines changed

.env

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,2 @@
1-
# General Configuration
2-
PORT=4010
3-
APP_NAME=ABC_NEWS_BLOG
4-
5-
NODE_ENV=local
6-
# NODE_ENV=prod
7-
8-
APP_BASE_URL=https://example.com/
9-
API_BASE_URL=http://localhost:4010/
10-
FRONTEND_BASE_URL=http://localhost:3000/
11-
12-
# DB Credentials
13-
DB_USER = karthik-dell
14-
DB_PASS = MzTNiZfCoo3Sl7lI
15-
DB_NAME = ecommerce
16-
DB_HOST = cluster0.bksct.mongodb.net
17-
18-
ACCESS_TOKEN_SECERT = MONGO_DB_NODE_JS_API
19-
PASSWORD_SALT = MONGO_DB_NODE_JS_API
20-
JWT_SECRET = MONGO_DB_NODE_JS_API
1+
# NODE_ENV=production
2+
NODE_ENV=development

.env.development

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# General Configuration
2+
PORT=4010
3+
APP_NAME=ECOMMERCE_APPLICATION
4+
5+
APP_BASE_URL=https://example.com/
6+
API_BASE_URL=http://localhost:4010/
7+
FRONTEND_BASE_URL=http://localhost:3000/
8+
9+
# DB Credentials
10+
DB_USER =
11+
DB_PASS =
12+
DB_NAME = ecommerce
13+
DB_HOST =
14+
15+
ACCESS_TOKEN_SECERT = MONGO_DB_NODE_JS_API
16+
PASSWORD_SALT = MONGO_DB_NODE_JS_API
17+
JWT_SECRET = MONGO_DB_NODE_JS_API

.gitignore

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,4 @@ node_modules/
33
jspm_packages/
44

55
# dotenv environment variable files
6-
.env.development.local
7-
.env.production.local
6+
.env.production

controller/category.controller.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const { UnauthorizedError, handleCustomError } = require("../utils/errors");
44
const { v4: uuidv4 } = require('uuid');
55

66
module.exports = {
7-
addcategory : async (req, res) => {
7+
addNewCategory : async (req, res) => {
88
const { title, parentId, image1 } = req.body;
99
try {
1010
let categoryId = uuidv4();

package-lock.json

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"dotenv": "^16.4.5",
66
"express": "^4.18.3",
77
"express-validator": "^7.0.1",
8+
"helmet": "^7.1.0",
89
"jsonwebtoken": "^9.0.2",
910
"mongoose": "^8.2.1",
1011
"morgan": "^1.10.0",

routes/admin.routes.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@ const router = express.Router();
33

44
router.use(require('./user.routes').userRouter)
55
router.use(require('./product.routes').adminRouter)
6+
router.use(require('./category.routes').adminRouter)
67

78
module.exports = router;

routes/category.routes.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
const express = require('express');
2+
const adminRouter = express.Router();
3+
const homeRouter = express.Router();
4+
5+
const categoryController = require('../controller/category.controller');
6+
// const { validateProduct } = require('../middleware/validation/productValidation');
7+
8+
adminRouter.post('/addCategory', categoryController.addNewCategory);
9+
adminRouter.get('/getProducts', categoryController.getCategory);
10+
homeRouter.get('/getProducts', categoryController.getCategory);
11+
12+
module.exports = {adminRouter, homeRouter};

server.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
const express = require('express');
22
const http = require('http');
33
const morgan = require('morgan');
4+
const helmet = require('helmet');
45

56
const app = express();
67
app.use(morgan('dev'))
8+
app.use(helmet());
79

810
const config = require('./utils/config')
911
require('./utils/express')(app);
1012
require('./utils/db')
1113
const server = http.createServer(app);
1214

1315
server.listen(config.PORT, () => {
14-
console.log(`${config.APP_NAME} ${config.ENVIRONMENT} server is running on port ${config.PORT}`);
16+
console.log(`${config.APP_NAME} ::: ${config.NODE_ENV} server is running on port ${config.PORT}`);
1517
});

services/product.service.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ module.exports = {
1414
},
1515
createNewVariants : async(item) => {
1616
try {
17-
const response = Variant.insertMany(item)
17+
const response = await Variant.insertMany(item)
1818
return { status:1, data:response }
1919
} catch (error) {
2020
console.error('Error in PRODUCT SERVICE :: createNewVariants : ', error);

utils/config.js

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,25 @@
1-
require('dotenv').config();
1+
require('dotenv').config()
2+
console.log('====================================');
3+
console.log("NODE_ENV :: ", process.env.NODE_ENV);
4+
console.log('====================================');
5+
require('dotenv').config({
6+
path: process.env.NODE_ENV === 'production' ? '.env.production' : '.env.development'
7+
});
28
module.exports = {
3-
ENVIRONMENT : process.env.NODE_ENV || 'development',
9+
NODE_ENV : process.env.NODE_ENV,
410
PORT : process.env.PORT || 4010,
511
APP_NAME : process.env.APP_NAME,
6-
SALT : process.env.PASSWORD_SALT,
12+
713
JWT_SECRET : process.env.JWT_SECRET,
814
PASSWORD_SALT : process.env.PASSWORD_SALT,
9-
ACCESS_TOKEN_SECERT : process.env.ACCESS_TOKEN_SECERT
15+
ACCESS_TOKEN_SECERT : process.env.ACCESS_TOKEN_SECERT.ACCESS_TOKEN_SECERT,
16+
17+
DB_USER : process.env.DB_USER,
18+
DB_PASS : process.env.DB_PASS,
19+
DB_NAME : process.env.DB_NAME,
20+
DB_HOST : process.env.DB_HOST,
21+
22+
APP_BASE_URL: process.env.APP_BASE_URL,
23+
API_BASE_URL: process.env.API_BASE_URL,
24+
FRONTEND_BASE_URL: process.env.FRONTEND_BASE_URL
1025
};

utils/db.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
const mongoose = require('mongoose');
2-
require('dotenv').config();
2+
const config = require('./config');
33

44
let db, uri, options
55

6-
const username = encodeURIComponent(process.env.DB_USER);
7-
const password = encodeURIComponent(process.env.DB_PASS);
8-
const cluster = process.env.DB_HOST;
9-
const dbname = process.env.DB_NAME;
6+
const username = encodeURIComponent(config.DB_USER);
7+
const password = encodeURIComponent(config.DB_PASS);
8+
const cluster = config.DB_HOST;
9+
const dbname = config.DB_NAME;
1010

11-
const SERVER = process.env.NODE_ENV
11+
const SERVER = config.NODE_ENV
1212

1313
if(SERVER.toLowerCase() === 'localhost' ||
1414
SERVER.toLowerCase() === 'local' ||

utils/express.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ const express = require('express');
22
const cors = require('cors');
33
const config = require('./config');
44
module.exports = (app) => {
5-
if (process.env.NODE_ENV !== 'local') {
6-
}
75
app.use(cors({
86
origin:[
97
'http://localhost:3000'

0 commit comments

Comments
 (0)