forked from akshat-sachan/EventM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
63 lines (53 loc) · 1.82 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
const express = require('express');
const app = express();
const cors = require('cors');
const morgan = require('morgan');
const mongoose = require('mongoose');
const userRoute = require('./routes/user');
const interestRoute = require('./routes/interest');
const sgMail = require('@sendgrid/mail');
require('dotenv').config();
app.use(cors());
app.use(express.json());
app.use(morgan());
app.use(express.urlencoded({ extended: true }));
sgMail.setApiKey(process.env.SENDGRID_API_KEY);
app.post('/api/user/send-registration-email', async (req, res) => {
try {
const { userEmail, eventId } = req.body;
if (!userEmail || !eventId) {
return res.status(400).json({ message: 'User email or event ID not provided in the request body' });
}
const msg = {
to: userEmail,
from: '[email protected]',
subject: 'Registration Confirmation',
text: 'Thank you for registering for our event! We look forward to seeing you there.',
html: '<strong>Thank you for registering for our event! We look forward to seeing you there.</strong>',
};
await sgMail.send(msg);
console.log('Email sent');
res.json({ message: 'Email sent successfully' });
} catch (error) {
console.error('Error sending email:', error);
res.status(500).json({ message: 'Error sending email' });
}
});
const port = process.env.PORT || 8000;
app.use('/api/user', userRoute);
app.use('/api/interest', interestRoute);
async function connectToDatabase() {
try {
await mongoose.connect(process.env.MONGO_URI, {
useNewUrlParser: true,
useUnifiedTopology: true,
});
console.log('Connected to MongoDB');
app.listen(port, () => {
console.log(`Example app listening on port ${port}!`);
});
} catch (err) {
console.error('Error connecting to MongoDB:', err);
}
}
connectToDatabase();