-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
51 lines (38 loc) · 1.54 KB
/
server.js
File metadata and controls
51 lines (38 loc) · 1.54 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
// src/server.js
// Lightweight HTTP server bootstrap for the API.
const express = require('express'); // Web framework
const cors = require('cors'); // Cross-origin requests
const killPort = require('kill-port'); // Ensures port is free before starting
require('dotenv').config(); // Load environment variables from .env
const app = express();
const PORT = process.env.PORT || 3000; // Prefer env var, fallback to 3000
// Global middleware
app.use(cors()); // Allow CORS for all routes
app.use(express.json()); // Parse JSON bodies
// Route modules (keep imports at top-level to attach below)
const ingestRoutes = require('./src/routes/ingest.routes');
const dataRoutes = require('./src/routes/data.routes');
const adminRoutes = require('./src/routes/admin.routes');
// Mount routes (mount order can matter if prefixes overlap)
app.use(ingestRoutes);
app.use(dataRoutes);
app.use(adminRoutes);
// Cron (scheduled NOAA sync)
const {startNOAACronJob} = require('./src/cron/syncData');
async function startServer(port) {
try {
// Avoid "EADDRINUSE" by killing any process already bound to the port
await killPort(port, 'tcp');
// Start HTTP server
app.listen(port, () => {
console.log(`Server is running on http://localhost:${port}`);
// Start cron AFTER server is listening so it can call /addallcountydata
startNOAACronJob();
});
} catch (err) {
console.error('Error starting server:', err);
}
}
// Entrypoint
startServer(PORT);
module.exports = app; // Export for testing or external usage