From 0e4ff625b231c4dca0284e1074a499bcba741998 Mon Sep 17 00:00:00 2001 From: Ujjwal Saini Date: Sat, 12 Oct 2024 17:34:04 +0530 Subject: [PATCH] Deploying Nodejs on versel --- Backend/api/index.js | 5 +++++ Backend/api/send-email.js | 46 +++++++++++++++++++++++++++++++++++++++ Backend/package.json | 3 ++- Backend/vercel.json | 7 ++++++ 4 files changed, 60 insertions(+), 1 deletion(-) create mode 100644 Backend/api/index.js create mode 100644 Backend/api/send-email.js create mode 100644 Backend/vercel.json diff --git a/Backend/api/index.js b/Backend/api/index.js new file mode 100644 index 0000000..3014b1d --- /dev/null +++ b/Backend/api/index.js @@ -0,0 +1,5 @@ +// api/index.js +export default function handler(req, res) { + res.status(200).json({ message: 'API is running' }); + } + \ No newline at end of file diff --git a/Backend/api/send-email.js b/Backend/api/send-email.js new file mode 100644 index 0000000..45c7d5e --- /dev/null +++ b/Backend/api/send-email.js @@ -0,0 +1,46 @@ +const nodemailer = require('nodemailer'); + +export default async function handler(req, res) { + if (req.method === 'POST') { + const { email } = req.body; + + if (!email) { + return res.status(400).json({ success: false, message: 'Email is required' }); + } + + // Set up nodemailer transporter + const transporter = nodemailer.createTransport({ + service: 'gmail', // You can use another service like SendGrid or SMTP + auth: { + user: 'saini.ujjwals007@gmail.com', + pass: 'pnot bnpc cfwp wnyx', + }, + }); + + const mailOptions = { + from: 'saini.ujjwals007@gmail.com', // Your email + to: email, + subject: 'Congratulations! You’ve Successfully Subscribed to Daily Bhagavad Gita Shlokas!', + text: `You have successfully subscribed to daily Bhagavad Gita Shlokas! Congratulations! 🎉 + +Get ready to dive deep into the profound wisdom and teachings of the Bhagavad Gita. Each day, you will receive a meaningful Shloka delivered straight to your inbox. These sacred verses will guide you on a spiritual journey, offering insights into life, duty, and inner peace. + +Enjoy the daily dose of timeless philosophy and find inspiration from the divine teachings of Lord Krishna. 🌿 + +We are excited to have you with us on this path of spiritual growth!` + }; + + try { + const info = await transporter.sendMail(mailOptions); + console.log('Email sent:', info.response); + return res.status(200).json({ success: true, message: 'Email sent successfully!' }); + } catch (error) { + console.error('Error sending email:', error); + return res.status(500).json({ success: false, message: 'Failed to send email', error }); + } + } else { + // Handle other HTTP methods + res.setHeader('Allow', ['POST']); + return res.status(405).end(`Method ${req.method} Not Allowed`); + } +} diff --git a/Backend/package.json b/Backend/package.json index 268e6f8..60dd51f 100644 --- a/Backend/package.json +++ b/Backend/package.json @@ -4,7 +4,8 @@ "description": "", "main": "index.js", "scripts": { - "test": "echo \"Error: no test specified\" && exit 1" + "test": "echo \"Error: no test specified\" && exit 1", + "dev": "vercel dev" }, "author": "Ujjwal Saini", "license": "ISC", diff --git a/Backend/vercel.json b/Backend/vercel.json new file mode 100644 index 0000000..f6875a8 --- /dev/null +++ b/Backend/vercel.json @@ -0,0 +1,7 @@ +{ + "version": 2, + "builds": [ + { "src": "api/**/*.js", "use": "@vercel/node" } + ] + } + \ No newline at end of file