From 1e6f0654875a2f2e73159e1964da75e0fc6a920e Mon Sep 17 00:00:00 2001 From: Suyog Jadhav Date: Wed, 9 Nov 2022 11:05:31 +0530 Subject: [PATCH] update config --- index.js | 92 +++++++++++++++++++++----------------------- modules/functions.js | 20 +++++----- 2 files changed, 54 insertions(+), 58 deletions(-) diff --git a/index.js b/index.js index 03c6d7e..03479a5 100644 --- a/index.js +++ b/index.js @@ -1,63 +1,59 @@ -const path = require('path'); -const express = require('express'); -const bodyParser = require('body-parser'); -var cors = require('cors'); -const { - MongoClient -} = require('mongodb') -const config = require('./config'); +const path = require("path"); +const express = require("express"); +const bodyParser = require("body-parser"); +var cors = require("cors"); +const { MongoClient } = require("mongodb"); const app = express(); -const port = config.port; // create application/json parser -app.use(bodyParser.urlencoded({ +app.use( + bodyParser.urlencoded({ extended: false, - limit: '500mb' -})); - + limit: "500mb", + }) +); //Assgin all routes -const routes = require('./routes'); -app.use('/', routes); +const routes = require("./routes"); +app.use("/", routes); //mongo db config -const client = new MongoClient(config.db, { - useNewUrlParser: true, - useUnifiedTopology: true -}) -client.connect() -const db = client.db('expressmongoapi') -app.set('client', db); +const client = new MongoClient(process.env.db, { + useNewUrlParser: true, + useUnifiedTopology: true, +}); +client.connect(); +const db = client.db("expressmongoapi"); +app.set("client", db); function clientClose(client) { - // client.close(); - return null; + // client.close(); + return null; } -app.set('clientClose', clientClose(client)); +app.set("clientClose", clientClose(client)); //cors -const allowlist = config.corsAllowList; +const allowlist = JSON.parse(process.env.corsAllowList); const corsOptionsDelegate = (req, callback) => { - let corsOptions; - - let isDomainAllowed = allowlist.indexOf(req.header('Origin')) !== -1; - - if (isDomainAllowed) { - // Enable CORS for this request - corsOptions = { - origin: true - } - } else { - // Disable CORS for this request - corsOptions = { - origin: false - } - } - callback(null, corsOptions) -} -app.use(cors(corsOptionsDelegate)) - -app.listen(port, () => { - console.log(`Example app listening on port ${port}!`) -}); \ No newline at end of file + let corsOptions; + + let isDomainAllowed = allowlist.indexOf(req.header("Origin")) !== -1; + + if (isDomainAllowed) { + // Enable CORS for this request + corsOptions = { + origin: true, + }; + } else { + // Disable CORS for this request + corsOptions = { + origin: false, + }; + } + callback(null, corsOptions); +}; +app.use(cors(corsOptionsDelegate)); +app.listen(process.env.PORT || 3000, () => { + console.log(`Example app listening on port ${process.env.PORT}!`); +}); diff --git a/modules/functions.js b/modules/functions.js index 38a7efa..fc740f2 100644 --- a/modules/functions.js +++ b/modules/functions.js @@ -5,7 +5,6 @@ const path = require('path'); var ObjectID = require('mongodb').ObjectID; const requestIp = require('request-ip'); const errors = require('../config/errors.json'); -const config = require('../config'); module.exports = { validateEmail: function (data) { @@ -125,7 +124,7 @@ module.exports = { }) } else { var cert = fs.readFileSync('key/public.key'); // get public key - var token = data.access_token.replace("tt", config.jwt_temp_algo); + var token = data.access_token.replace("tt", process.env.jwt_temp_algo); jwt.verify(token, cert, function (err, decoded) { if (err) { reject({ @@ -149,7 +148,7 @@ module.exports = { "message": errors.access_token_not_found_message }) } else { - var token = data.access_token.replace("tt", config.jwt_temp_algo); + var token = data.access_token.replace("tt", process.env.jwt_temp_algo); var decoded = jwt.decode(token); resolve(decoded); } @@ -165,7 +164,7 @@ module.exports = { }) } else { const secret = requestIp.getClientIp(req); - var token = data.access_token.replace("lt", config.jwt_algo); + var token = data.access_token.replace("lt", process.env.jwt_algo); jwt.verify(token, secret, function (err, decoded) { if (err) { reject({ @@ -192,7 +191,7 @@ module.exports = { makeValidTempAccessToken: function (data) { var privateKey = fs.readFileSync(`key/private.key`); var token = jwt.sign(data, privateKey, { - algorithm: config.jwt_temp_algo_name, + algorithm: process.env.jwt_temp_algo_name, expiresIn: '1h' }); var header = token.substr(0, token.indexOf(".")); @@ -239,17 +238,18 @@ module.exports = { }, sendMail: function (to, subject, message, path, token) { return new Promise(function (resolve, reject) { + var mail = JSON.parse(process.env.mail) var transporter = nodemailer.createTransport({ - host: config.mail.host, - port: config.mail.port, + host: mail.host, + port: mail.port, secure: true, auth: { - user: config.mail.username, - pass: config.mail.password + user: mail.username, + pass: mail.password } }); - var url = config.baseUrl + path + "?access_token=" + token; + var url = process.env.baseUrl + path + "?access_token=" + token; var mailOptions = { from: "no-reply@mapdatalab.com",