-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
61 lines (52 loc) · 1.99 KB
/
server.js
File metadata and controls
61 lines (52 loc) · 1.99 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
// Import dependencies
const express = require('express');
const nodemailer = require('nodemailer');
const path = require('path'); // Needed to serve static files
const cors = require('cors'); // Import cors for handling CORS issues
require('dotenv').config(); // Load environment variables from the .env file
const app = express();
const port = process.env.PORT || 3000;
// Enable CORS for your frontend’s URL
app.use(cors({
origin: 'https://timebridge.onrender.com', // Replace with your frontend's URL
methods: ['GET', 'POST'],
allowedHeaders: ['Content-Type']
}));
// Middleware to serve static files (such as index.html, CSS, etc.)
app.use(express.static(path.join(__dirname, 'public')));
// Nodemailer configuration using environment variables
const transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: process.env.EMAIL, // Environment variable
pass: process.env.PASSWORD // Environment variable
}
});
// Middleware to parse JSON
app.use(express.json()); // Recommended to use express.json() for modern versions
// Route to handle email sending
app.post('/send-email', (req, res) => {
const { email1, email2, message } = req.body;
const mailOptions = {
from: process.env.EMAIL, // Use the email from environment variables
to: `${email1}, ${email2}`, // Recipients
subject: 'Notification about your event', // Subject
text: message // Email body
};
// Send the email
transporter.sendMail(mailOptions, (error, info) => {
if (error) {
console.error('Error sending email:', error); // Print the error on the server console
return res.status(500).send('Error sending email: ' + error.toString());
}
res.status(200).send('Email sent: ' + info.response);
});
});
// Route to handle all GET requests (including "/")
app.get('*', (req, res) => {
res.sendFile(path.join(__dirname, 'public', 'index.html'));
});
// Start the server on port
app.listen(port, () => {
console.log(`Server running at http://localhost:${port}`);
});