-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
35 lines (30 loc) · 941 Bytes
/
server.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
const express = require('express');
const mongoose = require('mongoose');
const cors = require('cors');
const dotenv = require('dotenv');
const Team = require('./models/Team'); // Make sure the path is correct
dotenv.config();
const app = express();
app.use(cors());
app.use(express.json());
mongoose.connect(process.env.MONGO_URI, {
useNewUrlParser: true,
useUnifiedTopology: true
})
.then(() => console.log('MongoDB connected successfully'))
.catch(err => {
console.error('Failed to connect to MongoDB:', err);
});
// Route to get all teams
app.get('/api/teams', (req, res) => {
Team.find({})
.then(teams => {
console.log(teams); // Log to verify backend data structure
res.json(teams);
})
.catch(err => res.status(500).json({ error: err.message }));
});
const PORT = process.env.PORT || 5001;
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});