- Add GET /api/users/:id for profile retrieval with stats - Add PATCH /api/users/:id for profile updates - Add GET /api/users/:id/reports to fetch user's reports - Add GET /api/users/:id/events to fetch user's events (registered & organized) - Add PATCH /api/users/:id/preferences for notification settings - Add POST /api/users/:id/calculate-impact for impact score calculation - Create userService with all business logic - Create userController with request handling - Create userRoutes and register in main server - Update frontend userService with new endpoints - Add comprehensive documentation in USER_PROFILE_ENDPOINTS.md - Add test script for manual endpoint testing Requirements satisfied: 6.3, 6.4, 6.5, 10.4
78 lines
2.0 KiB
TypeScript
78 lines
2.0 KiB
TypeScript
import express from 'express';
|
|
import cors from 'cors';
|
|
import helmet from 'helmet';
|
|
import dotenv from 'dotenv';
|
|
import path from 'path';
|
|
import reportRoutes from './routes/reportRoutes';
|
|
import eventRoutes from './routes/eventRoutes';
|
|
import userRoutes from './routes/userRoutes';
|
|
|
|
// Load environment variables
|
|
dotenv.config();
|
|
|
|
const app = express();
|
|
const PORT = process.env.PORT || 3005;
|
|
|
|
// Middleware
|
|
app.use(helmet({
|
|
crossOriginResourcePolicy: { policy: "cross-origin" },
|
|
contentSecurityPolicy: false, // Disable CSP for development
|
|
}));
|
|
app.use(cors({
|
|
origin: [
|
|
'http://localhost:5173',
|
|
'http://localhost:5174',
|
|
process.env.CORS_ORIGIN || ''
|
|
].filter(Boolean),
|
|
credentials: true,
|
|
}));
|
|
app.use(express.json());
|
|
app.use(express.urlencoded({ extended: true }));
|
|
|
|
// Serve uploaded files
|
|
app.use('/uploads', express.static(path.join(__dirname, '../../uploads')));
|
|
|
|
// Health check
|
|
app.get('/health', (req, res) => {
|
|
res.json({ status: 'ok', timestamp: new Date().toISOString() });
|
|
});
|
|
|
|
// API Routes
|
|
app.use('/api/reports', reportRoutes);
|
|
app.use('/api/events', eventRoutes);
|
|
app.use('/api/users', userRoutes);
|
|
|
|
// Error handling middleware
|
|
app.use((err: any, req: express.Request, res: express.Response, next: express.NextFunction) => {
|
|
console.error('Error:', err);
|
|
|
|
if (err.name === 'MulterError') {
|
|
if (err.code === 'LIMIT_FILE_SIZE') {
|
|
return res.status(400).json({
|
|
error: {
|
|
code: 'FILE_TOO_LARGE',
|
|
message: 'File size exceeds the maximum limit of 5MB',
|
|
timestamp: new Date().toISOString(),
|
|
},
|
|
});
|
|
}
|
|
}
|
|
|
|
res.status(500).json({
|
|
error: {
|
|
code: 'INTERNAL_SERVER_ERROR',
|
|
message: err.message || 'An unexpected error occurred',
|
|
timestamp: new Date().toISOString(),
|
|
},
|
|
});
|
|
});
|
|
|
|
// Start server
|
|
app.listen(PORT, () => {
|
|
console.log(`🚀 Server running on port ${PORT}`);
|
|
console.log(`📍 Environment: ${process.env.NODE_ENV || 'development'}`);
|
|
console.log(`🔗 API: http://localhost:${PORT}/api`);
|
|
});
|
|
|
|
export default app;
|