-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
60 lines (49 loc) · 1.7 KB
/
server.js
File metadata and controls
60 lines (49 loc) · 1.7 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
const express = require('express');
const cors = require('cors');
const path = require('path');
//require('dotenv').config();
// Import route modules
const flightRoutes = require('./routes/flightRoutes');
const airportRoutes = require('./routes/airportRoutes');
const config = require('./config/api');
const app = express();
// Configure middleware
app.use(cors({
origin: '*',
methods: ['GET', 'POST', 'PUT', 'DELETE'],
allowedHeaders: ['Content-Type', 'Authorization']
}));
app.use(express.json());
// Serve static files from the root directory
app.use(express.static('./'));
// Import configuration from config folder
// Register API routes with configuration
app.use('/api/flights', flightRoutes);
app.use('/api/airports', airportRoutes);
// Log that API is configured
console.log('https://api.aviationstack.com/v1/', config.API_URL);
// Basic route for testing
app.get('/api/test', (req, res) => {
res.json({ message: 'Server is working!' });
});
// Error handling middleware
app.use((err, req, res, next) => {
console.error('Server error:', err);
res.status(500).json({ error: 'Internal server error', message: err.message });
});
// Handle 404 errors
app.use((req, res) => {
res.status(404).json({ error: 'Not found', message: `Route ${req.originalUrl} not found` });
});
// Start the server
const PORT = process.env.PORT || 5000;
app.listen(PORT, () => {
console.log(`Server running on port ${PORT}`);
console.log(`Test URL: http://localhost:${PORT}/api/test`);
}).on('error', (err) => {
if (err.code === 'EADDRINUSE') {
console.error(`⚠️ Port ${PORT} is already in use. Try stopping other services or using a different port.`);
} else {
console.error('Server startup error:', err);
}
});