forked from Yashwanthmne/InnKeep
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
99 lines (89 loc) · 2.33 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
// server.js
require("dotenv").config();
var fs = require("fs");
var request_create = "";
fs.readFile("request_creation_mail.html", function(err, data) {
console.log({ fs_data: data });
request_create = data;
console.log({ request_create });
});
var request_update = "";
fs.readFile("request_update_mail.html", function(err, data) {
console.log({ fs_data: data });
request_update = data;
console.log({ request_update });
});
var express = require("express");
var fallback = require("express-history-api-fallback");
const root = `${__dirname}/dist`;
// var path = require("path");
// var serveStatic = require("serve-static");
var cors = require("cors");
const nodemailer = require("nodemailer");
const mail_config = {
host: "smtp.gmail.com",
port: 465,
secure: true,
auth: {
user: process.env.NOTIFICATION_EMAIL,
pass: process.env.NOTIFICATION_EMAIL_PASSWORD
}
};
console.log({ mail_config });
const transporter = nodemailer.createTransport(mail_config);
const sendEmail = ({ to, subject, data }) => {
console.log({ request_create: request_create });
return new Promise((resolve, reject) => {
transporter.sendMail(
{
from: process.env.NOTIFICATION_EMAIL,
to: to,
subject: subject,
html:
data.create === true
? request_create
.toString()
.replace("__url__", `<a href="${data.url}">${data.url}</a>`)
: request_update.toString()
},
(error, data) => {
if (error) {
console.log(error);
reject(error);
}
console.log("Sent!", data);
resolve(data);
}
);
});
};
const app = express();
// app.use(history({
// verbose: true
// }))
app.use(cors({ origin: "*" }));
app.use(express.json());
// app.use(serveStatic(__dirname + "/dist"));
app.use(express.static(root));
app.use(fallback("index.html", { root }));
app.post("/send-notification", async (req, res) => {
// console.log('qwerty', req);
let context = req.body;
console.log({ body: context });
sendEmail(context)
.then(data => {
res.send({
status: 200,
data: data
});
})
.catch(error => {
res.send({
status: 500,
data: error
});
});
});
var port = process.env.PORT || 5000;
app.listen(port);
console.log("server started " + port);