-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
299 lines (248 loc) · 7.6 KB
/
server.js
File metadata and controls
299 lines (248 loc) · 7.6 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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
const express = require('express');
const bcrypt = require('bcryptjs');
const cors = require('cors');
const helmet = require('helmet');
const rateLimit = require('express-rate-limit');
const path = require('path');
const { PrismaClient } = require('@prisma/client');
const app = express();
const prisma = new PrismaClient();
const PORT = process.env.PORT || 5500;
// Security middleware
app.use(helmet({
contentSecurityPolicy: false, // Disable for development
}));
// Rate limiting
const limiter = rateLimit({
windowMs: 15 * 60 * 1000, // 15 minutes
max: 100, // limit each IP to 100 requests per windowMs
message: 'Too many requests from this IP, please try again later.'
});
app.use(limiter);
// CORS
app.use(cors({
origin: process.env.NODE_ENV === 'production' ? 'your-domain.com' : true,
credentials: true
}));
// Body parsing
app.use(express.json({ limit: '10mb' }));
app.use(express.urlencoded({ extended: true }));
// Static files
app.use(express.static(path.join(__dirname, 'public')));
// Auth middleware
const authenticateUser = async (req, res, next) => {
const userId = req.header('x-user-id');
if (!userId) {
return res.status(401).json({ error: 'Authentication required' });
}
try {
const user = await prisma.user.findUnique({
where: { id: parseInt(userId) }
});
if (!user) {
return res.status(401).json({ error: 'Invalid user' });
}
req.user = user;
next();
} catch (error) {
res.status(500).json({ error: 'Authentication error' });
}
};
// Routes
// Health check
app.get('/api/health', (req, res) => {
res.json({ status: 'OK', timestamp: new Date().toISOString() });
});
// Auth routes
app.post('/api/auth/register', async (req, res) => {
try {
const { email, password, name } = req.body;
// Validation
if (!email || !password) {
return res.status(400).json({ error: 'Email and password are required' });
}
if (password.length < 6) {
return res.status(400).json({ error: 'Password must be at least 6 characters' });
}
// Check if user exists
const existingUser = await prisma.user.findUnique({
where: { email: email.toLowerCase() }
});
if (existingUser) {
return res.status(400).json({ error: 'User already exists' });
}
// Hash password
const hashedPassword = await bcrypt.hash(password, 12);
// Create user
const user = await prisma.user.create({
data: {
email: email.toLowerCase(),
password: hashedPassword,
name: name || null
},
select: {
id: true,
email: true,
name: true,
createdAt: true
}
});
res.status(201).json({
message: 'User created successfully',
user
});
} catch (error) {
console.error('Registration error:', error);
res.status(500).json({ error: 'Internal server error' });
}
});
app.post('/api/auth/login', async (req, res) => {
try {
const { email, password } = req.body;
if (!email || !password) {
return res.status(400).json({ error: 'Email and password are required' });
}
// Find user
const user = await prisma.user.findUnique({
where: { email: email.toLowerCase() }
});
if (!user) {
return res.status(401).json({ error: 'Invalid credentials' });
}
// Check password
const isValidPassword = await bcrypt.compare(password, user.password);
if (!isValidPassword) {
return res.status(401).json({ error: 'Invalid credentials' });
}
res.json({
message: 'Login successful',
user: {
id: user.id,
email: user.email,
name: user.name
}
});
} catch (error) {
console.error('Login error:', error);
res.status(500).json({ error: 'Internal server error' });
}
});
// Task routes
app.get('/api/tasks', authenticateUser, async (req, res) => {
try {
const { sortBy = 'createdAt', sortOrder = 'desc' } = req.query;
const orderBy = {};
if (sortBy === 'dueDate') {
orderBy.dueDate = sortOrder;
} else if (sortBy === 'priority') {
orderBy.priority = sortOrder;
} else {
orderBy.createdAt = sortOrder;
}
const tasks = await prisma.task.findMany({
where: { userId: req.user.id },
orderBy,
select: {
id: true,
title: true,
description: true,
dueDate: true,
priority: true,
completed: true,
createdAt: true,
updatedAt: true
}
});
res.json(tasks);
} catch (error) {
console.error('Get tasks error:', error);
res.status(500).json({ error: 'Internal server error' });
}
});
app.post('/api/tasks', authenticateUser, async (req, res) => {
try {
const { title, description, dueDate, priority } = req.body;
if (!title || title.trim().length === 0) {
return res.status(400).json({ error: 'Title is required' });
}
const task = await prisma.task.create({
data: {
title: title.trim(),
description: description?.trim() || null,
dueDate: dueDate ? new Date(dueDate) : null,
priority: priority || 'MEDIUM',
userId: req.user.id
}
});
res.status(201).json(task);
} catch (error) {
console.error('Create task error:', error);
res.status(500).json({ error: 'Internal server error' });
}
});
app.put('/api/tasks/:id', authenticateUser, async (req, res) => {
try {
const { id } = req.params;
const { title, description, dueDate, priority, completed } = req.body;
// Check if task exists and belongs to user
const existingTask = await prisma.task.findFirst({
where: { id: parseInt(id), userId: req.user.id }
});
if (!existingTask) {
return res.status(404).json({ error: 'Task not found' });
}
const updateData = {};
if (title !== undefined) updateData.title = title.trim();
if (description !== undefined) updateData.description = description?.trim() || null;
if (dueDate !== undefined) updateData.dueDate = dueDate ? new Date(dueDate) : null;
if (priority !== undefined) updateData.priority = priority;
if (completed !== undefined) updateData.completed = completed;
const updatedTask = await prisma.task.update({
where: { id: parseInt(id) },
data: updateData
});
res.json(updatedTask);
} catch (error) {
console.error('Update task error:', error);
res.status(500).json({ error: 'Internal server error' });
}
});
app.delete('/api/tasks/:id', authenticateUser, async (req, res) => {
try {
const { id } = req.params;
// Check if task exists and belongs to user
const existingTask = await prisma.task.findFirst({
where: { id: parseInt(id), userId: req.user.id }
});
if (!existingTask) {
return res.status(404).json({ error: 'Task not found' });
}
await prisma.task.delete({
where: { id: parseInt(id) }
});
res.status(204).send();
} catch (error) {
console.error('Delete task error:', error);
res.status(500).json({ error: 'Internal server error' });
}
});
// Serve the main HTML file
app.get('*', (req, res) => {
res.sendFile(path.join(__dirname, 'public', 'index.html'));
});
// Error handling middleware
app.use((error, req, res, next) => {
console.error('Unhandled error:', error);
res.status(500).json({ error: 'Internal server error' });
});
// Start server
app.listen(PORT, () => {
console.log(`🚀 Server running on http://localhost:${PORT}`);
console.log(`📊 Health check: http://localhost:${PORT}/api/health`);
});
// Graceful shutdown
process.on('SIGINT', async () => {
console.log('\n🛑 Shutting down gracefully...');
await prisma.$disconnect();
process.exit(0);
});