|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const express = require('express'); |
| 4 | +const cookieParser = require('cookie-parser'); |
| 5 | +const path = require('path'); |
| 6 | + |
| 7 | +const authRoutes = require('./src/routes/auth'); |
| 8 | +const referralRoutes = require('./src/routes/referral'); |
| 9 | + |
| 10 | +const app = express(); |
| 11 | +const PORT = process.env.PORT || 3000; |
| 12 | + |
| 13 | +// Middleware |
| 14 | +app.use(express.json()); |
| 15 | +app.use(express.urlencoded({ extended: false })); |
| 16 | +app.use(cookieParser()); |
| 17 | + |
| 18 | +// Serve static frontend files |
| 19 | +app.use(express.static(path.join(__dirname, 'public'))); |
| 20 | + |
| 21 | +// API routes |
| 22 | +app.use('/api/auth', authRoutes); |
| 23 | +app.use('/api/referral', referralRoutes); |
| 24 | + |
| 25 | +// Catch-all: serve index.html for client-side navigation |
| 26 | +app.get('*', (req, res) => { |
| 27 | + res.sendFile(path.join(__dirname, 'public', 'index.html')); |
| 28 | +}); |
| 29 | + |
| 30 | +// Only start listening when run directly (not during tests) |
| 31 | +if (require.main === module) { |
| 32 | + if (!process.env.JWT_SECRET) { |
| 33 | + console.error('ERROR: JWT_SECRET environment variable is not set. Refusing to start.'); |
| 34 | + process.exit(1); |
| 35 | + } |
| 36 | + app.listen(PORT, () => { |
| 37 | + console.log(`Evodron Referral server running on http://localhost:${PORT}`); |
| 38 | + }); |
| 39 | +} |
| 40 | + |
| 41 | +module.exports = app; |
0 commit comments