-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
147 lines (120 loc) · 3.76 KB
/
index.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
const express = require("express");
const dotenv = require("dotenv");
const morgan = require("morgan");
const cors = require("cors");
const session = require("express-session");
const swaggerJsDoc = require("swagger-jsdoc");
const swaggerUI = require("swagger-ui-express");
const nodemailer = require("nodemailer");
const https = require("https"); // Import the https module
dotenv.config();
const app = express();
app.use(express.json({ limit: "50mb" }));
app.use(express.urlencoded({ limit: "50mb", extended: true }));
if (process.env.NODE_ENV === "development") {
app.use(morgan("dev"));
}
app.use(session({ secret: "secret" }));
const swaggerOptions = {
swaggerDefinition: {
info: {
title: "Techx mail API",
version: "1.0.0",
},
},
apis: ["/nodemailer/swagger.json"],
};
const swaggerDocs = swaggerJsDoc(swaggerOptions);
app.use("/api-docs", swaggerUI.serve, swaggerUI.setup(swaggerDocs));
app.use(cors());
app.use(function (req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header(
"Access-Control-Allow-Headers",
"Origin, X-Requested-With, Content-Type, Accept"
);
next();
});
const swaggerUi = require("swagger-ui-express");
const swaggerDocument = require("./swagger.json");
app.get("/health", (req, res) => {
res.status(200).send(" sendmail backend is up and running");
});
app.use("/api-docs", swaggerUi.serve, swaggerUi.setup(swaggerDocument));
app.get("/", (req, res) => {
res.status(200).send(`<h1>Welcome to techxmail</h1>`);
});
app.get("*", (req, res) => {
res.status(200).send(`<b>Not found</b>`);
});
// Email validation function using regex
function isValidEmail(email) {
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
return emailRegex.test(email);
}
app.post("/sendmail", (req, res) => {
const { mail, subject, text, html, name } = req.body;
// Validate email format
if (!isValidEmail(mail)) {
res.status(400).send({ message: "Invalid email format" });
return;
}
if (!mail || !subject || !name) {
res.status(400).send({ message: "Incomplete data" });
return;
}
var transporter = nodemailer.createTransport({
service: "gmail", // name of email provider
auth: {
user: "[email protected]", // sender's gmail id
pass: process.env.pass, // sender password
},
});
const from = `Techx Mail Service`;
var mailOptions = {
from: from,
to: mail,
subject: `${subject} `,
text: text || null,
html: html || null,
};
try {
transporter.sendMail(mailOptions, function (error, info) {
if (error) {
console.log(error);
res.status(500).send({ message: error.message });
} else {
console.log("Email sent: " + info.response);
res.status(200).send({ message: "Email sent successfully" });
}
});
} catch (e) {
console.log(e);
res.status(500).send({ message: e.message });
}
});
// Function to check the health of the server every 2 minutes using https
const healthCheckInterval = setInterval(() => {
https.get("https://techxmail.onrender.com/health", (resp) => {
let data = '';
// A chunk of data has been received
resp.on('data', (chunk) => {
data += chunk;
});
// The whole response has been received
resp.on('end', () => {
console.log(`Health check passed: ${data}`);
});
}).on("error", (error) => {
console.error("Health check failed:", error.message);
// You can add a response or log the error here
});
}, 120000); // 120000 milliseconds = 2 minutes
// Clearing the interval on server shutdown
process.on('SIGINT', () => {
clearInterval(healthCheckInterval);
console.log('Health check interval cleared.');
process.exit();
});
const port = process.env.PORT || 5000;
app.listen(port, console.log(`Server running on port ${port}`));