-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
68 lines (57 loc) · 1.96 KB
/
server.js
File metadata and controls
68 lines (57 loc) · 1.96 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
const express = require('express');
const dotenv = require('dotenv');
const mongoose = require('mongoose');
const cors = require('cors');
// Load environment variables
dotenv.config();
// Initialize express
const app = express();
// Middleware
app.use(cors());
app.use(express.json());
// Determine if using Atlas or local MongoDB
const isAtlasURI = process.env.MONGO_URI.includes('mongodb+srv');
console.log(`Attempting to connect to ${isAtlasURI ? 'MongoDB Atlas' : 'local MongoDB'}`);
// Connect to MongoDB
mongoose.connect(process.env.MONGO_URI, {
useNewUrlParser: true,
useUnifiedTopology: true,
serverSelectionTimeoutMS: 5000 // Timeout after 5 seconds
})
.then(() => {
console.log('💾 MongoDB Connected successfully');
})
.catch(err => {
console.error('❌ MongoDB Connection Error:', err);
if (isAtlasURI) {
console.log('\n🔍 Troubleshooting Atlas connection:');
console.log('1. Check if your IP is whitelisted in MongoDB Atlas');
console.log('2. Verify username and password are correct');
console.log('3. Try switching to local MongoDB by updating your .env file:\n');
console.log(' MONGO_URI=mongodb://localhost:27017/taskmanager\n');
} else {
console.log('\n🔍 Troubleshooting local MongoDB connection:');
console.log('1. Make sure MongoDB is installed and running');
console.log('2. Check if MongoDB service is started:');
console.log(' - Run "net start MongoDB" in Command Prompt as Administrator');
console.log('3. Verify MongoDB is listening on default port 27017\n');
}
});
// Import routes
const taskRoutes = require('./routes/taskRoutes');
// Basic route
app.get('/', (req, res) => {
res.status(200).json({
success: true,
message: 'Welcome to the Task Manager API',
version: '1.0.0'
});
});
// Mount routes
app.use('/api/tasks', taskRoutes);
// Set port
const PORT = process.env.PORT || 5000;
// Start server
app.listen(PORT, () => {
console.log(`🚀 Server running on port ${PORT}`);
});