-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathserver.js
52 lines (45 loc) · 1.33 KB
/
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import express from 'express';
import mongoose from 'mongoose';
import bodyParser from 'body-parser';
import cors from 'cors';
import dotenv from 'dotenv';
import Student from './models/Student.js';
dotenv.config();
const app = express();
const PORT = process.env.PORT || 5000;
// Middleware
app.use(cors());
app.use(bodyParser.json());
// Connect to MongoDB Atlas using the URI from the .env file
mongoose.connect(process.env.MONGODB_URI, {
useNewUrlParser: true,
useUnifiedTopology: true,
})
.then(() => console.log('MongoDB Atlas connected'))
.catch(err => console.error('MongoDB connection error:', err));
// Route to add student data
app.post('/api/students', async (req, res) => {
console.log("Received data:", req.body);
try {
const student = new Student(req.body);
await student.save();
res.status(201).send(student);
} catch (error) {
console.error("Error saving student:", error);
res.status(400).send(error);
}
});
// Route to get all students
app.get('/api/students', async (req, res) => {
try {
const students = await Student.find();
res.status(200).send(students);
} catch (error) {
console.error("Error fetching students:", error);
res.status(500).send(error);
}
});
// Start the server
app.listen(PORT, () => {
console.log(`Server is running on http://localhost:${PORT}`);
});