-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
116 lines (101 loc) · 3.19 KB
/
server.js
File metadata and controls
116 lines (101 loc) · 3.19 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
const express = require('express');
const mongoose = require('mongoose');
const cors = require('cors');
const helmet = require('helmet');
const rateLimit = require('express-rate-limit');
require('dotenv').config();
const app = express();
// Security middleware
app.use(helmet({
crossOriginEmbedderPolicy: false,
contentSecurityPolicy: false
}));
// CORS configuration - Allow all origins in development
app.use(cors({
origin: true, // Allow all origins in development
credentials: true,
methods: ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS'],
allowedHeaders: ['Content-Type', 'Authorization']
}));
// Rate limiting
const limiter = rateLimit({
windowMs: 15 * 60 * 1000, // 15 minutes
max: 100 // limit each IP to 100 requests per windowMs
});
app.use('/api/', limiter);
// Body parsing middleware
app.use(express.json({ limit: '50mb' }));
app.use(express.urlencoded({ extended: true, limit: '50mb' }));
// Static files for video streaming with proper CORS headers
app.use('/videos', cors({
origin: true,
credentials: false,
methods: ['GET', 'HEAD', 'OPTIONS']
}), (req, res, next) => {
// Set proper MIME type for video files
if (req.path.endsWith('.mp4')) {
res.type('video/mp4');
} else if (req.path.endsWith('.webm')) {
res.type('video/webm');
} else if (req.path.endsWith('.avi')) {
res.type('video/x-msvideo');
}
res.header('Accept-Ranges', 'bytes');
next();
}, express.static('uploads/raw'));
app.use('/thumbnails', cors({
origin: true,
credentials: false,
methods: ['GET', 'HEAD', 'OPTIONS']
}), (req, res, next) => {
// Set proper MIME type for image files
if (req.path.endsWith('.svg')) {
res.type('image/svg+xml');
} else if (req.path.endsWith('.jpg') || req.path.endsWith('.jpeg')) {
res.type('image/jpeg');
} else if (req.path.endsWith('.png')) {
res.type('image/png');
}
next();
}, express.static('uploads/thumbnails'));
// Database connection
mongoose.connect(process.env.MONGODB_URI, {
useNewUrlParser: true,
useUnifiedTopology: true,
})
.then(() => console.log('✅ MongoDB connected successfully'))
.catch(err => console.error('❌ MongoDB connection error:', err));
// Routes
app.use('/api/auth', require('./routes/auth'));
app.use('/api/videos', require('./routes/videos'));
app.use('/api/users', require('./routes/users'));
app.use('/api/playlists', require('./routes/playlists'));
app.use('/api/search', require('./routes/search'));
app.use('/api/admin', require('./routes/admin'));
app.use('/api/youtube', require('./routes/youtube'));
// Health check
app.get('/health', (req, res) => {
res.json({
status: 'OK',
timestamp: new Date().toISOString(),
uptime: process.uptime()
});
});
// Error handling middleware
app.use((err, req, res, next) => {
console.error(err.stack);
res.status(500).json({
message: 'Something went wrong!',
error: process.env.NODE_ENV === 'development' ? err.message : {}
});
});
// 404 handler
app.use('*', (req, res) => {
res.status(404).json({ message: 'Route not found' });
});
const PORT = process.env.PORT || 5000;
app.listen(PORT, () => {
console.log(`🚀 PlayCast server running on port ${PORT}`);
console.log(`📱 Environment: ${process.env.NODE_ENV}`);
});
module.exports = app;