-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
54 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}!`); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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: "[email protected]", | ||
|