Skip to content

Commit

Permalink
update config
Browse files Browse the repository at this point in the history
  • Loading branch information
jsuyog2 committed Nov 9, 2022
1 parent 315604f commit 1e6f065
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 58 deletions.
92 changes: 44 additions & 48 deletions index.js
Original file line number Diff line number Diff line change
@@ -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}!`)
});
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}!`);
});
20 changes: 10 additions & 10 deletions modules/functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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({
Expand All @@ -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);
}
Expand All @@ -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({
Expand All @@ -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("."));
Expand Down Expand Up @@ -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: "[email protected]",
Expand Down

0 comments on commit 1e6f065

Please sign in to comment.