Skip to content

Commit

Permalink
Update server.js
Browse files Browse the repository at this point in the history
  • Loading branch information
adityakalburgi authored Oct 10, 2024
1 parent a7eeccf commit b81ebbf
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion backend/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,14 @@ app.post('/api/logout', (req, res) => {
res.json({ message: 'Logout successful' }); // Send a JSON response
});
});
// Centralized Error Handling Middleware
app.use((err, req, res, next) => {
console.error(err.stack); // Log the error stack trace for debugging purposes
res.status(500).json({
message: 'An internal server error occurred',
error: process.env.NODE_ENV === 'development' ? err.message : {} // Only send the error message in development mode
});
});

// Start the server
const PORT = process.env.PORT || 5000;
Expand All @@ -122,4 +130,4 @@ process.on('SIGINT', async () => {
console.log('Shutting down gracefully...');
await mongoose.connection.close();
process.exit(0);
});
});

0 comments on commit b81ebbf

Please sign in to comment.