-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
40 lines (35 loc) · 1.05 KB
/
index.js
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
const express = require('express');
const dotenv = require('dotenv');
const mongoose = require('mongoose');
const cors = require('cors');
const userRoutes = require('./src/routes/userRoutes');
const initiativeRoutes = require('./src/routes/initiativeRoutes');
const resourceRoutes = require('./src/routes/resourceRoutes');
dotenv.config();
const app = express();
// Middleware
app.use(express.json());
app.use(cors());
// Routes
app.use('/api/users', userRoutes);
app.use('/api/initiatives', initiativeRoutes);
app.use('/api/resources', resourceRoutes);
// Error Handling
app.use((err, req, res, next) => {
console.error(err.stack);
res.status(500).send('Internal Server Error');
});
// Connect to MongoDB
mongoose.connect(process.env.MONGODB_URI, {
useNewUrlParser: true,
useUnifiedTopology: true
})
.then(() => {
console.log('Connected to MongoDB');
// Start server
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log(`Server running on port ${PORT}`);
});
})
.catch(err => console.error('Error connecting to MongoDB:', err));