diff --git a/backend/controllers/ContactController.js b/backend/controllers/ContactController.js new file mode 100644 index 00000000..bfb0e5c9 --- /dev/null +++ b/backend/controllers/ContactController.js @@ -0,0 +1,15 @@ + +const ContactUs = require('../models/ContactUs'); + +exports.submitContactForm = async (req, res) => { + const { name, email, message } = req.body; + + try { + const newContact = new ContactUs({ name, email, message }); + await newContact.save(); + return res.status(201).json({ message: 'Message sent successfully!' }); + } catch (error) { + console.error('Error saving contact form:', error); + return res.status(500).json({ message: 'Failed to send message. Please try again later.' }); + } +}; diff --git a/backend/index.js b/backend/index.js index efdce6d9..09a7bba3 100644 --- a/backend/index.js +++ b/backend/index.js @@ -2,6 +2,7 @@ const express = require('express'); const connectDB = require('./config/db'); // Database connection const authRoutes = require('./routes/authRoutes'); // Authentication routes +const ContactRoutes = require('./routes/ContactRoutes'); const cookieParser = require('cookie-parser'); // Middleware for parsing cookies const config = require('./config/config'); // Config file for environment variables const cors = require('cors'); // Middleware for Cross-Origin Resource Sharing @@ -14,7 +15,7 @@ connectDB(); // CORS configuration app.use(cors({ - origin: 'http://your-frontend-domain.com', // Replace with your frontend URL + origin: 'http://127.0.0.1:5504', // Correct: specify the base URL only credentials: true // Allow credentials (cookies) to be sent with requests })); @@ -23,7 +24,8 @@ app.use(express.json()); // Parse JSON bodies app.use(cookieParser()); // Enable cookie parsing // API routes -app.use('/api/auth', authRoutes); // Route for authentication endpoints +app.use('/api/auth', authRoutes); +app.use('/api/contact', ContactRoutes); // Server listening on configured port const PORT = config.port || 5000; // Use config for port, default to 5000 if undefined diff --git a/backend/models/ContactUs.js b/backend/models/ContactUs.js new file mode 100644 index 00000000..7bfad587 --- /dev/null +++ b/backend/models/ContactUs.js @@ -0,0 +1,23 @@ +// models/Contact.js +const mongoose = require('mongoose'); + +const ContactSchema = new mongoose.Schema({ + name: { + type: String, + required: true, + trim: true, + }, + email: { + type: String, + + trim: true, + match: /.+\@.+\..+/, + }, + message: { + type: String, + required: true, + trim: true, + }, +}, { timestamps: true }); + +module.exports = mongoose.model('Contact', ContactSchema); diff --git a/backend/package-lock.json b/backend/package-lock.json index 148533ba..c797da78 100644 --- a/backend/package-lock.json +++ b/backend/package-lock.json @@ -187,7 +187,6 @@ "version": "2.8.5", "resolved": "https://registry.npmjs.org/cors/-/cors-2.8.5.tgz", "integrity": "sha512-KIHbLJqu73RGr/hnbrO9uBeixNGuvSQjul/jdFvS/KFSIH1hWVd1ng7zOHx+YrEfInLG7q4n6GHQ9cDtxv/P6g==", - "license": "MIT", "dependencies": { "object-assign": "^4", "vary": "^1" @@ -316,7 +315,6 @@ "version": "4.21.1", "resolved": "https://registry.npmjs.org/express/-/express-4.21.1.tgz", "integrity": "sha512-YSFlK1Ee0/GC8QaO91tHcDxJiE/X4FbpAyQWkxAvG6AXCuR65YzK8ua6D9hvi/TzUfZMpc+BwuM1IPw8fmQBiQ==", - "license": "MIT", "dependencies": { "accepts": "~1.3.8", "array-flatten": "1.1.1", diff --git a/backend/routes/ContactRoutes.js b/backend/routes/ContactRoutes.js new file mode 100644 index 00000000..f1e713e6 --- /dev/null +++ b/backend/routes/ContactRoutes.js @@ -0,0 +1,9 @@ +const express = require('express'); +const { submitContactForm } = require('../controllers/ContactController.js'); + + +const router = express.Router(); + +router.post('/', submitContactForm); + +module.exports = router; diff --git a/index.html b/index.html index 4a783a2f..6d130fc2 100644 --- a/index.html +++ b/index.html @@ -2251,23 +2251,27 @@

About BuddyTrail

Send Us a Message

-
- + +
- +