-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver-temp.js
More file actions
271 lines (237 loc) · 7.03 KB
/
server-temp.js
File metadata and controls
271 lines (237 loc) · 7.03 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
const express = require('express');
const cors = require('cors');
const path = require('path');
const app = express();
const PORT = process.env.PORT || 5000;
// Middleware
app.use(cors());
app.use(express.json());
app.use(express.static(path.join(__dirname)));
// Temporary in-memory data (replaces MongoDB for now)
const tempMovies = [
{
_id: '1',
title: "Stranger Things",
description: "When a young boy disappears, his mother, a police chief and his friends must confront terrifying supernatural forces in order to get him back.",
releaseYear: 2016,
genre: ["Drama", "Fantasy", "Horror"],
duration: "45-150 min",
rating: 8.7,
posterUrl: "https://image.tmdb.org/t/p/w500/49WJfeN0moxb9IPfGn8AIqMGskD.jpg",
bannerUrl: "https://image.tmdb.org/t/p/original/56v2KjBlU4XaEv9u8Enq2qwYyf0.jpg",
isTrending: true,
isPopular: true,
isNew: false
},
{
_id: '2',
title: "The Witcher",
description: "Geralt of Rivia, a solitary monster hunter, struggles to find his place in a world where people often prove more wicked than beasts.",
releaseYear: 2019,
genre: ["Action", "Adventure", "Drama"],
duration: "60 min",
rating: 8.2,
posterUrl: "https://image.tmdb.org/t/p/w500/7vjaCdMw15FEbXyLQTVa04URsPm.jpg",
bannerUrl: "https://image.tmdb.org/t/p/original/jBJWaqoSCiARWtfVzGlfXKyHeFz.jpg",
isTrending: true,
isPopular: true,
isNew: false
},
{
_id: '3',
title: "Wednesday",
description: "Smart, sarcastic and a little dead inside, Wednesday Addams investigates a murder spree while making new friends at Nevermore Academy.",
releaseYear: 2022,
genre: ["Comedy", "Crime", "Fantasy"],
duration: "45-60 min",
rating: 8.1,
posterUrl: "https://image.tmdb.org/t/p/w500/9PFonBhy4cQy7Jz20NpMygczOkv.jpg",
bannerUrl: "https://image.tmdb.org/t/p/original/9Gtg2DzBhmYamXBS1hKAhiwbB6I.jpg",
isTrending: true,
isPopular: true,
isNew: true
}
];
// Temporary in-memory users
const tempUsers = [];
// Routes
app.get('/api/movies/trending', (req, res) => {
try {
const trendingMovies = tempMovies.filter(movie => movie.isTrending);
res.json({
success: true,
count: trendingMovies.length,
data: trendingMovies
});
} catch (error) {
res.status(500).json({
success: false,
message: 'Error fetching trending movies'
});
}
});
app.get('/api/movies', (req, res) => {
try {
const { page = 1, limit = 20, genre, search } = req.query;
let filteredMovies = [...tempMovies];
// Filter by genre
if (genre) {
filteredMovies = filteredMovies.filter(movie =>
movie.genre.some(g => g.toLowerCase().includes(genre.toLowerCase()))
);
}
// Search by title or description
if (search) {
filteredMovies = filteredMovies.filter(movie =>
movie.title.toLowerCase().includes(search.toLowerCase()) ||
movie.description.toLowerCase().includes(search.toLowerCase())
);
}
// Pagination
const startIndex = (page - 1) * limit;
const endIndex = startIndex + parseInt(limit);
const paginatedMovies = filteredMovies.slice(startIndex, endIndex);
res.json({
success: true,
data: paginatedMovies,
totalPages: Math.ceil(filteredMovies.length / limit),
currentPage: parseInt(page),
total: filteredMovies.length
});
} catch (error) {
res.status(500).json({
success: false,
message: 'Error fetching movies'
});
}
});
app.get('/api/movies/search', (req, res) => {
try {
const { q, genre, year, rating } = req.query;
let filteredMovies = [...tempMovies];
if (q) {
filteredMovies = filteredMovies.filter(movie =>
movie.title.toLowerCase().includes(q.toLowerCase()) ||
movie.description.toLowerCase().includes(q.toLowerCase())
);
}
if (genre) {
filteredMovies = filteredMovies.filter(movie =>
movie.genre.some(g => g.toLowerCase().includes(genre.toLowerCase()))
);
}
if (year) {
filteredMovies = filteredMovies.filter(movie => movie.releaseYear === parseInt(year));
}
if (rating) {
filteredMovies = filteredMovies.filter(movie => movie.rating >= parseFloat(rating));
}
res.json({
success: true,
count: filteredMovies.length,
data: filteredMovies
});
} catch (error) {
res.status(500).json({
success: false,
message: 'Error searching movies'
});
}
});
app.get('/api/movies/:id', (req, res) => {
try {
const movie = tempMovies.find(m => m._id === req.params.id);
if (!movie) {
return res.status(404).json({
success: false,
message: 'Movie not found'
});
}
res.json({
success: true,
data: movie
});
} catch (error) {
res.status(500).json({
success: false,
message: 'Error fetching movie'
});
}
});
// Simple auth endpoints (temporary)
app.post('/api/auth/signup', (req, res) => {
try {
const { email, password, username } = req.body;
// Check if user exists
const existingUser = tempUsers.find(u => u.email === email || u.username === username);
if (existingUser) {
return res.status(400).json({
success: false,
message: 'User already exists'
});
}
// Create user (in real app, hash password)
const newUser = {
id: Date.now().toString(),
email,
username,
isAdmin: false
};
tempUsers.push(newUser);
res.status(201).json({
success: true,
message: 'User created successfully',
data: {
user: newUser,
token: 'temp-token-' + Date.now()
}
});
} catch (error) {
res.status(500).json({
success: false,
message: 'Error creating user'
});
}
});
app.post('/api/auth/login', (req, res) => {
try {
const { email, password } = req.body;
const user = tempUsers.find(u => u.email === email);
if (!user) {
return res.status(401).json({
success: false,
message: 'Invalid credentials'
});
}
res.json({
success: true,
message: 'Login successful',
data: {
user,
token: 'temp-token-' + Date.now()
}
});
} catch (error) {
res.status(500).json({
success: false,
message: 'Error during login'
});
}
});
// Serve frontend
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, 'index.html'));
});
// Error handling middleware
app.use((err, req, res, next) => {
console.error(err.stack);
res.status(500).json({ message: 'Something went wrong!' });
});
app.listen(PORT, () => {
console.log(`🚀 Temporary Server running on port ${PORT}`);
console.log(`🌐 Frontend: http://localhost:${PORT}`);
console.log(`🔌 API: http://localhost:${PORT}/api`);
console.log(`🔥 Test Trending: http://localhost:${PORT}/api/movies/trending`);
console.log(`\n📝 This is a temporary server without MongoDB.`);
console.log(`📝 Check MONGODB_SETUP.md for permanent setup options.`);
});