This repository was archived by the owner on Apr 8, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
97 lines (88 loc) · 2.4 KB
/
server.js
File metadata and controls
97 lines (88 loc) · 2.4 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
const express = require('express');
const cors = require('cors');
const helmet = require('helmet');
const morgan = require('morgan');
const path = require('path');
// Import routes
const fieldRoutes = require('./routes/fields');
const comparisonRoutes = require('./routes/comparisons');
const app = express();
const PORT = process.env.PORT || 3000;
// Middleware
app.use(helmet());
app.use(cors());
app.use(morgan('combined'));
app.use(express.json());
app.use(express.static(path.join(__dirname, 'public')));
// Routes
app.use('/api/fields', fieldRoutes);
app.use('/api/comparisons', comparisonRoutes);
// Health check endpoint
app.get('/health', (req, res) => {
res.json({
status: 'OK',
message: 'CIA Factbook API is running',
timestamp: new Date().toISOString()
});
});
// Root endpoint with API documentation
app.get('/', (req, res) => {
res.json({
message: 'CIA World Factbook API',
version: '1.0.0',
endpoints: {
fields: '/api/fields',
comparisons: '/api/comparisons',
health: '/health'
},
availableFields: [
'diplomatic-representation-us',
'economic-overview',
'gdp-purchasing-power',
'electricity-access',
'civil-aircraft-code',
'military-and-security-forces',
'climate',
'urbanization',
'major-urban-areas-population',
'drinking-water-source',
'sanitation-facility-access',
'education-expenditure',
'environmental-issues'
],
availableComparisons: [
'subscriptions-comparison',
'electricity-country-comparison',
'energy-country-comparison',
'airports-country-comparison'
]
});
});
// Error handling middleware
app.use((err, req, res, next) => {
console.error(err.stack);
res.status(500).json({
success: false,
error: 'Internal server error',
timestamp: new Date().toISOString()
});
});
// 404 handler
app.use((req, res) => {
res.status(404).json({
success: false,
error: 'Endpoint not found',
message: 'Please check the API documentation for available endpoints',
availableEndpoints: {
root: '/',
health: '/health',
fields: '/api/fields',
comparisons: '/api/comparisons'
},
timestamp: new Date().toISOString()
});
});
app.listen(PORT, () => {
console.log(`🚀 CIA Factbook API server running on port ${PORT}`);
console.log(`📊 API documentation available at http://localhost:${PORT}`);
});