-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
107 lines (88 loc) · 2.96 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
const express = require('express');
const cors = require('cors');
const connectDB = require('./config/db');
const path = require('path');
const nodemailer = require("nodemailer");
const fs = require('fs');
const handlebars = require('handlebars');
const { encrypt } = require('./server/middleware/EncryptDecrypt');
const Quiz = require('./server/models/quiz.models');
//init middleware
const app = express();
app.use(cors());
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
//connect to DB
connectDB();
//routes
app.use('/api/auth', require('./server/routes/auth.routes'));
app.use('/api/employer', require('./server/routes/employer.routes'));
app.use('/api/quiz', require('./server/routes/quiz.routes'));
app.use('/api/respondent', require('./server/routes/respondent.routes'));
if(process.env.NODE_ENV === 'production') {
app.use(express.static('client/build'));
app.get('*', (req, res) =>
res.sendFile(path.resolve(__dirname, 'client', 'build', 'index.html'))
);
}
//init server connection
const PORT = process.env.PORT || 7000;
app.listen(PORT, () => {
console.log(`Listening on port ${PORT}`);
});
var readHTMLFile = (path, callback) => {
fs.readFile(path, {encoding: 'utf-8'}, (err, html) => {
if(err) {
callback(err);
throw err;
} else {
callback(null, html);
}
});
};
app.post("/send/:quizId", async(req, res) => {
const quiz_id = req.params.quizId;
const quizTitle = await Quiz.findById(quiz_id);
const { email } = req.body;
const emailHash = encrypt(email);
var url = '';
if(process.env.NODE_ENV === 'production') {
url = `${req.protocol}://${req.hostname}/${emailHash.iv}/userInfo/${emailHash.content}/quiz/${quiz_id}`;
} else {
url = `${req.protocol}://${req.hostname}:3000/${emailHash.iv}/userInfo/${emailHash.content}/quiz/${quiz_id}`;
}
const transporter = nodemailer.createTransport({
service: "gmail",
auth: {
user: "[email protected]",
pass: "OSUcapston1111",
},
});
readHTMLFile(__dirname + '/client/src/email/RespondentEmail.html', (err, html) => {
var template = handlebars.compile(html);
var replacements = {
quizTitle: quizTitle.title,
url: url
};
var htmlToSend = template(replacements);
const mailOptions = {
from: "[email protected]",
to: req.body.email,
subject: `${req.body.name}`,
html: htmlToSend,
attachments: [{
filename: 'logo_small.png',
path: __dirname + '/client/src/assets/logo_small.png',
cid: 'banana'
}]
};
transporter.sendMail(mailOptions, (error, info) => {
if (error) {
console.log(error);
res.json({ msg: error });
} else {
res.json({ msg: "success" });
}
});
})
});