forked from muhammad-hassaan-y2/CurioKids
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.ts
More file actions
77 lines (64 loc) · 2.79 KB
/
Copy pathserver.ts
File metadata and controls
77 lines (64 loc) · 2.79 KB
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
import { createServer, IncomingMessage, ServerResponse } from 'http';
import { parse } from 'url';
import { StringDecoder } from 'string_decoder';
import nodemailer from 'nodemailer';
import dotenv from 'dotenv';
// Load environment variables from a `.env` file
dotenv.config();
const PORT = process.env.PORT || 5000;
const handleRequest = (req: IncomingMessage, res: ServerResponse) => {
const parsedUrl = parse(req.url || '', true);
const decoder = new StringDecoder('utf-8');
let body = '';
req.on('data', (chunk) => {
body += decoder.write(chunk);
});
req.on('end', () => {
body += decoder.end();
if (req.method === 'POST' && parsedUrl.pathname === '/send-email') {
try {
const { email, feedbackType, title, steps, priority } = JSON.parse(body);
// Validate required fields
if (!email || !feedbackType || !title || !steps || !priority) {
res.writeHead(400, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ message: 'Missing required fields' }));
return;
}
// Configure Nodemailer transporter
const transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: process.env.GMAIL_USER, // Replace with your Gmail address from environment variables
pass: process.env.GMAIL_PASS, // Replace with your Gmail password or app password from environment variables
},
});
const mailOptions = {
from: email,
to: process.env.GMAIL_USER, // Replace with your email address from environment variables
subject: `New Feedback: ${feedbackType} - ${title}`,
text: `Feedback Type: ${feedbackType}\nTitle: ${title}\nSteps to Reproduce: ${steps}\nPriority Level: ${priority}\nSubmitted by: ${email}`,
};
// Send email
transporter.sendMail(mailOptions, (error, info) => {
if (error) {
res.writeHead(500, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ message: 'Failed to send email', error }));
} else {
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ message: 'Email sent successfully', info }));
}
});
} catch (error) {
res.writeHead(500, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ message: 'Failed to process request', error }));
}
} else {
res.writeHead(404, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ message: 'Route not found' }));
}
});
};
const server = createServer(handleRequest);
server.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});