Skip to content

Commit

Permalink
Merge branch 'main' into fixblog
Browse files Browse the repository at this point in the history
  • Loading branch information
AsifQamar authored Oct 27, 2024
2 parents ee275cb + b8487ae commit 5801846
Show file tree
Hide file tree
Showing 10 changed files with 284 additions and 170 deletions.
15 changes: 15 additions & 0 deletions backend/controllers/ContactController.js
Original file line number Diff line number Diff line change
@@ -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.' });
}
};
6 changes: 4 additions & 2 deletions backend/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}));

Expand All @@ -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
Expand Down
23 changes: 23 additions & 0 deletions backend/models/ContactUs.js
Original file line number Diff line number Diff line change
@@ -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);
7 changes: 5 additions & 2 deletions backend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"license": "ISC",
"description": "",
"dependencies": {
"backend": "file:",
"bcryptjs": "^2.4.3",
"cookie-parser": "^1.4.7",
"cors": "^2.8.5",
Expand Down
9 changes: 9 additions & 0 deletions backend/routes/ContactRoutes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const express = require('express');
const { submitContactForm } = require('../controllers/ContactController.js');


const router = express.Router();

router.post('/', submitContactForm);

module.exports = router;
Loading

0 comments on commit 5801846

Please sign in to comment.