-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
79 lines (71 loc) · 2.24 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
const express = require("express");
const app = express();
const mysql = require("mysql");
const db = require("./models");
const path = require("path");
const routes = require("./routes/contact-routes")
const routesHtml = require('./routes/html-routes')
const PORT = process.env.PORT || 4000;
const dotenv = require("dotenv");
const cors = require("cors");
const nodemailer = require("nodemailer");
const sequelize = require('sequelize')
dotenv.config();
app.use(cors());
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
app.use(express.static(path.join(__dirname, 'client/build')));
const corsOptions = {
origin: "http://kionling.herokuapp.com",
optionsSuccessStatus: 200,
};
app.use(cors(corsOptions));
// Other routes and middleware
app.use(routes);
app.use(routesHtml)
function sendMail(recipient, subject, message) {
return new Promise((resolve, reject) => {
const transporter = nodemailer.createTransport({
service: "Gmail",
auth: {
user: process.env.EMAIL_USERNAME,
pass: process.env.EMAIL_PASSWORD
},
});
const mailOptions = {
from: recipient, // Sender address
to: process.env.DESTINATION_EMAIL, // Recipient address
subject: subject, // Email subjecta
text: message,
html: `<h1>${recipient}</h1><p>${subject}</p><p>${message}</p>` // Plain text body
};
transporter.sendMail(mailOptions, (error, info) => {
if (error) {
console.log("Error:", error);
res.status(500).send("Error sending email.");
reject(error);
} else {
console.log("Email sent:", info.response);
res.status(200).send("Email sent successfully.");
resolve(info);
}
});
});
}
app.post("/send_email", (req, res) => {
const { recipient, subject, message } = req.body;
sendMail(recipient, subject, message)
.then(() => res.status(200).send("Email sent successfully."))
.catch((error) => {
console.log("Error:", error);
res.status(500).send("Error sending email.");
});
});
if (process.env.NODE_ENV === "production") {
app.use(express.static("client/build"));
}
db.sequelize.sync().then(function () {
app.listen(PORT, () => {
console.log(`🙌😈🌝 app is now listening on ${PORT}`);
});
});