|
1 | 1 | const express = require('express'); |
2 | 2 | const cors = require('cors'); |
3 | 3 | const verifyToken = require('./middleware/verifyToken'); |
| 4 | +require('dotenv').config(); |
4 | 5 |
|
5 | 6 | const app = express(); |
6 | | -app.use(cors()); |
7 | | -app.use(express.json()); |
8 | 7 |
|
9 | | -// A protected route |
| 8 | +// --------------------------------------------------------------------------- |
| 9 | +// Security: Restrict CORS to known origins in production |
| 10 | +// --------------------------------------------------------------------------- |
| 11 | +const allowedOrigins = process.env.CORS_ALLOWED_ORIGINS |
| 12 | + ? process.env.CORS_ALLOWED_ORIGINS.split(',').map((o) => o.trim()) |
| 13 | + : ['http://localhost:5173', 'http://localhost:3000']; |
| 14 | + |
| 15 | +app.use( |
| 16 | + cors({ |
| 17 | + origin(origin, callback) { |
| 18 | + // Allow requests with no origin (e.g. server-to-server, curl) |
| 19 | + if (!origin || allowedOrigins.includes(origin)) { |
| 20 | + callback(null, true); |
| 21 | + } else { |
| 22 | + callback(new Error(`CORS: origin ${origin} not allowed`)); |
| 23 | + } |
| 24 | + }, |
| 25 | + methods: ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS'], |
| 26 | + allowedHeaders: ['Content-Type', 'Authorization'], |
| 27 | + credentials: true, |
| 28 | + }) |
| 29 | +); |
| 30 | + |
| 31 | +// --------------------------------------------------------------------------- |
| 32 | +// Body parsing with size limit to prevent large-payload attacks |
| 33 | +// --------------------------------------------------------------------------- |
| 34 | +app.use(express.json({ limit: '1mb' })); |
| 35 | + |
| 36 | +// --------------------------------------------------------------------------- |
| 37 | +// Security headers |
| 38 | +// --------------------------------------------------------------------------- |
| 39 | +app.use((_req, res, next) => { |
| 40 | + res.setHeader('X-Content-Type-Options', 'nosniff'); |
| 41 | + res.setHeader('X-Frame-Options', 'DENY'); |
| 42 | + res.setHeader('X-XSS-Protection', '1; mode=block'); |
| 43 | + res.removeHeader('X-Powered-By'); |
| 44 | + next(); |
| 45 | +}); |
| 46 | + |
| 47 | +// --------------------------------------------------------------------------- |
| 48 | +// Health check (public) |
| 49 | +// --------------------------------------------------------------------------- |
| 50 | +app.get('/', (_req, res) => { |
| 51 | + res.json({ status: 'ok', timestamp: new Date().toISOString() }); |
| 52 | +}); |
| 53 | + |
| 54 | +app.get('/health', (_req, res) => { |
| 55 | + res.json({ status: 'ok', timestamp: new Date().toISOString() }); |
| 56 | +}); |
| 57 | + |
| 58 | +// --------------------------------------------------------------------------- |
| 59 | +// Protected routes |
| 60 | +// --------------------------------------------------------------------------- |
10 | 61 | app.get('/profile', verifyToken, (req, res) => { |
11 | 62 | res.json({ message: `Hello user ${req.user.uid}` }); |
12 | 63 | }); |
13 | 64 |
|
14 | | -// Another public route just to test |
15 | | -app.get('/', (req, res) => { |
16 | | - res.send('Backend is running!'); |
| 65 | +// --------------------------------------------------------------------------- |
| 66 | +// 404 handler |
| 67 | +// --------------------------------------------------------------------------- |
| 68 | +app.use((_req, res) => { |
| 69 | + res.status(404).json({ error: 'Route not found' }); |
17 | 70 | }); |
18 | 71 |
|
| 72 | +// --------------------------------------------------------------------------- |
| 73 | +// Global error handler |
| 74 | +// --------------------------------------------------------------------------- |
| 75 | +app.use((err, _req, res, _next) => { |
| 76 | + console.error('Unhandled error:', err.message); |
| 77 | + const status = err.status || 500; |
| 78 | + res.status(status).json({ |
| 79 | + error: |
| 80 | + process.env.NODE_ENV === 'production' |
| 81 | + ? 'Internal server error' |
| 82 | + : err.message, |
| 83 | + }); |
| 84 | +}); |
| 85 | + |
| 86 | +// --------------------------------------------------------------------------- |
| 87 | +// Start server |
| 88 | +// --------------------------------------------------------------------------- |
19 | 89 | const PORT = process.env.PORT || 5000; |
20 | | -app.listen(PORT, () => console.log(`Server running on port ${PORT}`)); |
| 90 | +app.listen(PORT, () => { |
| 91 | + console.log(`Server running on port ${PORT} (${process.env.NODE_ENV || 'development'})`); |
| 92 | +}); |
0 commit comments