-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
82 lines (68 loc) · 2.97 KB
/
server.js
File metadata and controls
82 lines (68 loc) · 2.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
// Load environment variables from a .env file into process.env
require('dotenv').config();
// Import required packages
const express = require('express'); // Web framework for building APIs
const mongoose = require('mongoose'); // MongoDB object modeling tool
const cors = require('cors'); // Enables Cross-Origin Resource Sharing
const helmet = require('helmet'); // Adds extra security headers to HTTP responses
// Import routes
const authRoutes = require('./routes/auth'); // Authentication-related routes
const taskRoutes = require('./routes/tasks'); // Task management routes
// Import custom middleware
const errorHandler = require('./middleware/errorHandler'); // Global error handler
const auth = require('./middleware/auth'); // Authentication middleware
// Initialize Express app
const app = express();
// Apply security middleware
app.use(helmet()); // Protects app from common security vulnerabilities
app.use(cors()); // Allows requests from different origins (frontend apps, etc.)
// Apply body parsing middleware
app.use(express.json()); // Parse incoming JSON request bodies
app.use(express.urlencoded({ extended: true })); // Parse URL-encoded form data
// Connect to MongoDB database
mongoose.connect(process.env.MONGODB_URI) // Connection string stored in .env file
.then(() => console.log('✅ Connected to MongoDB'))
.catch(err => console.error('❌ MongoDB connection error:', err));
// Define routes
app.use('/api/auth', authRoutes); // Public auth routes (register/login)
app.use('/api/tasks', auth, taskRoutes); // Task routes (protected by auth middleware)
// Root route (helpful for testing and documentation)
app.get('/', (req, res) => {
res.json({
message: 'Task Management API is running!',
timestamp: new Date().toISOString(),
endpoints: {
// Auth endpoints
register: 'POST /api/auth/register',
login: 'POST /api/auth/login',
profile: 'GET /api/auth/me',
logout: 'POST /api/auth/logout',
// Task endpoints (require authentication)
getTasks: 'GET /api/tasks',
createTask: 'POST /api/tasks',
getTask: 'GET /api/tasks/:id',
updateTask: 'PUT /api/tasks/:id',
deleteTask: 'DELETE /api/tasks/:id',
getStats: 'GET /api/tasks/stats/overview'
}
});
});
// Health check endpoint (useful for monitoring and load balancers)
app.get('/api/health', (req, res) => {
res.status(200).json({
status: 'OK',
timestamp: new Date().toISOString()
});
});
// Global error handling middleware (must be defined last)
app.use(errorHandler);
// Catch-all for undefined routes (404 Not Found)
app.use((req, res) => {
res.status(404).json({ error: 'Route not found' });
});
// Start server on given port (from env or default to 3000)
const PORT = process.env.PORT || 3000;
app.listen(PORT, "0.0.0.0", () => {
console.log(`🚀 Server running on port ${PORT}`);
console.log(`📖 Visit: http://localhost:${PORT}`);
});