-
Notifications
You must be signed in to change notification settings - Fork 0
/
Server.js
77 lines (64 loc) · 2.03 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
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
// server.js
const express = require('express');
const mongoose = require('mongoose');
const cors = require('cors');
const bodyParser = require('body-parser');
const app = express();
app.use(cors());
const PORT = process.env.PORT || 5000;
// Connect to MongoDB Atlas
mongoose.connect('mongodb+srv://rsenapati:[email protected]/Schedule?retryWrites=true&w=majority')
.then(() => console.log('MongoDB connected'))
.catch(err => console.log(err));
const flightSchema = new mongoose.Schema({
email: {
type: String,
required: true
},
arrivalDateTime: {
type: Date,
required: true
},
airport: {
type: String,
required: true
}
});
const Flight = mongoose.model('Flight', flightSchema);
app.use(bodyParser.json());
// API endpoint to handle email submission
// API endpoint to handle flight arrival submission
app.post('/api/flights', async (req, res) => {
const { email, arrivalDateTime, airport } = req.body;
try {
// Save flight arrival data to database
const newFlight = new Flight({ email, arrivalDateTime, airport });
await newFlight.save();
res.status(201).json({ message: 'Flight arrival data saved successfully' });
} catch (error) {
console.error(error);
res.status(500).json({ message: 'Server error' });
}
});
// Example using Express.js
app.get('/filterData', async (req, res) => {
try {
const { duration, airport, email } = req.query;
// Parse flight time and duration
console.log(2)
const threshold= duration
// Query MongoDB to filter data
console.log(threshold)
const filteredData = await Flight.find({
airport,
arrivalDateTime: { $lt: threshold }
}, { email: 1, arrivalDateTime: 1 });
res.json(filteredData);
} catch (error) {
console.error('Error:', error);
res.status(500).json({ error: 'Internal Server Error' });
}
});
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});