Skip to content

Commit

Permalink
Merge pull request #810 from Ananya54321/patch-1
Browse files Browse the repository at this point in the history
Update server.js with better error handling
  • Loading branch information
SUGAM-ARORA authored Oct 16, 2024
2 parents 2b2889c + 35f3ae6 commit 4dfeaf5
Showing 1 changed file with 85 additions and 25 deletions.
110 changes: 85 additions & 25 deletions BACKEND/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,13 @@ mongoose.connect('mongodb://localhost:27017/stopwatch', {
});

const db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));

// Handle connection errors and ensure the process exits on failure
db.on('error', (error) => {
console.error('Connection error:', error);
process.exit(1); // Exit the process if there's a database connection error
});

db.once('open', () => {
console.log('Connected to MongoDB');
});
Expand All @@ -38,69 +44,123 @@ const recordingSchema = new mongoose.Schema({

const Recording = mongoose.model('Recording', recordingSchema);

// Endpoint to save recording time
// Endpoint to save recording time with error handling for missing data
app.post('/api/recordings', async (req, res) => {
const { time } = req.body;

if (!time) {
return res.status(400).json({ message: 'Time value is required.' });
}

const newRecording = new Recording({ time });
try {
const savedRecording = await newRecording.save();
res.status(201).json(savedRecording);
} catch (error) {
res.status(400).json({ message: error.message });
console.error('Error saving recording:', error);
res.status(500).json({ message: 'Failed to save the recording.' });
}
});

// Profile settings endpoint
// Profile settings endpoint with error handling for missing data
app.get('/api/profile', (req, res) => {
res.json(profileData);
try {
res.json(profileData);
} catch (error) {
console.error('Error fetching profile data:', error);
res.status(500).json({ message: 'Failed to fetch profile data.' });
}
});

app.put('/api/profile', (req, res) => {
profileData = req.body;
res.json({ message: 'Profile settings updated successfully!' });
const { username, bio } = req.body;

if (!username || !bio) {
return res.status(400).json({ message: 'Username and bio are required.' });
}

try {
profileData = req.body;
res.json({ message: 'Profile settings updated successfully!' });
} catch (error) {
console.error('Error updating profile data:', error);
res.status(500).json({ message: 'Failed to update profile settings.' });
}
});

// Account settings endpoint
// Account settings endpoint with error handling for missing data
app.get('/api/account', (req, res) => {
res.json(accountData);
try {
res.json(accountData);
} catch (error) {
console.error('Error fetching account data:', error);
res.status(500).json({ message: 'Failed to fetch account data.' });
}
});

app.put('/api/account', (req, res) => {
accountData = req.body;
res.json({ message: 'Account settings updated successfully!' });
const { email, password, language } = req.body;

if (!email || !password) {
return res.status(400).json({ message: 'Email and password are required.' });
}

try {
accountData = req.body;
res.json({ message: 'Account settings updated successfully!' });
} catch (error) {
console.error('Error updating account data:', error);
res.status(500).json({ message: 'Failed to update account settings.' });
}
});

// Endpoint to handle file uploads
// File upload endpoint with error handling for missing data
app.post('/send', upload.single('attachments'), (req, res) => {
const { name, email, issue, message } = req.body;
const attachment = req.file;

console.log('Received:', { name, email, issue, message, attachment });
if (!name || !email || !issue || !message) {
return res.status(400).json({ success: false, message: 'All fields are required.' });
}

// Simulate processing the data
if (name && email && issue && message) {
res.json({ success: true });
} else {
res.json({ success: false });
if (!attachment) {
return res.status(400).json({ success: false, message: 'Attachment is required.' });
}

try {
console.log('Received:', { name, email, issue, message, attachment });
res.json({ success: true, message: 'File and data received successfully.' });
} catch (error) {
console.error('Error processing request:', error);
res.status(500).json({ success: false, message: 'Failed to process the request.' });
}
});

// Endpoint to handle feedback submission
// Endpoint to handle feedback submission with error handling for missing data
app.post('/api/submit-feedback', (req, res) => {
const { emoji, feedback } = req.body;

// Here you would typically save the feedback to a database
// For now, we'll just log it to the console
console.log(`Received feedback: Emoji: ${emoji}, Feedback: ${feedback}`);
if (!emoji || !feedback) {
return res.status(400).json({ message: 'Emoji and feedback are required.' });
}

// Send a success response
res.status(200).send({ message: 'Feedback received successfully!' });
try {
console.log(`Received feedback: Emoji: ${emoji}, Feedback: ${feedback}`);
res.status(200).send({ message: 'Feedback received successfully!' });
} catch (error) {
console.error('Error submitting feedback:', error);
res.status(500).json({ message: 'Failed to submit feedback.' });
}
});

// Add a root route to handle the root URL
app.get('/', (req, res) => {
res.send('Welcome to the Stopwatch API!');
try {
res.send('Welcome to the Stopwatch API!');
} catch (error) {
console.error('Error handling root URL request:', error);
res.status(500).json({ message: 'Failed to load the homepage.' });
}
});

// Start the server
Expand Down

0 comments on commit 4dfeaf5

Please sign in to comment.