const express = require('express'); const cors = require('cors'); const helmet = require('helmet'); const rateLimit = require('express-rate-limit'); const path = require('path'); const config = require('./config'); // Import routes const authRoutes = require('./routes/auth'); const sellerAuthRoutes = require('./routes/seller-auth'); const customerRoutes = require('./routes/customer'); const sellerRoutes = require('./routes/seller'); const adminRoutes = require('./routes/admin'); const app = express(); // Security middleware app.use(helmet({ contentSecurityPolicy: { directives: { defaultSrc: ["'self'"], styleSrc: ["'self'", "'unsafe-inline'", "https://cdn.jsdelivr.net", "https://cdnjs.cloudflare.com"], scriptSrc: ["'self'", "'unsafe-inline'", "https://cdn.jsdelivr.net", "https://cdnjs.cloudflare.com"], scriptSrcAttr: ["'unsafe-inline'"], imgSrc: ["'self'", "data:", "https:", "http:"], connectSrc: ["'self'", "https:", "http:"], fontSrc: ["'self'", "https://cdnjs.cloudflare.com"], objectSrc: ["'none'"], upgradeInsecureRequests: [], }, }, })); // 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 configuration app.use(cors({ origin: [ 'http://localhost:3000', 'http://127.0.0.1:3000', 'https://shashwatidr-ecom.hf.space', 'https://*.hf.space' ], credentials: true })); // Body parsing middleware app.use(express.json({ limit: '10mb' })); app.use(express.urlencoded({ extended: true, limit: '10mb' })); // Serve static files app.use('/uploads', express.static(path.join(__dirname, 'uploads'))); // Serve HTML files app.get('/customer', (req, res) => { res.sendFile(path.join(__dirname, 'public/customer/index.html')); }); app.get('/seller', (req, res) => { res.sendFile(path.join(__dirname, 'public/seller/index.html')); }); app.get('/admin', (req, res) => { res.sendFile(path.join(__dirname, 'public/admin/index.html')); }); // Health check endpoint app.get('/api/health', (req, res) => { res.status(200).json({ status: 'OK', timestamp: new Date().toISOString(), uptime: process.uptime() }); }); // API routes app.use('/api/auth', authRoutes); app.use('/api/seller-auth', sellerAuthRoutes); app.use('/api/customer', customerRoutes); app.use('/api/seller', sellerRoutes); app.use('/api/admin', adminRoutes); // Root endpoint - redirect to customer page app.get('/', (req, res) => { res.redirect('/customer'); }); // 404 handler app.use('*', (req, res) => { res.status(404).json({ error: 'Route not found' }); }); // Error handling middleware app.use((error, req, res, next) => { console.error('Error:', error); if (error.code === 'LIMIT_FILE_SIZE') { return res.status(400).json({ error: 'File too large' }); } if (error.code === 'LIMIT_UNEXPECTED_FILE') { return res.status(400).json({ error: 'Unexpected field' }); } res.status(500).json({ error: config.NODE_ENV === 'production' ? 'Internal server error' : error.message }); }); const PORT = config.PORT; app.listen(PORT, () => { console.log(`🚀 Server running on port ${PORT}`); console.log(`📱 Customer: http://localhost:${PORT}/customer`); console.log(`🏪 Seller: http://localhost:${PORT}/seller`); console.log(`👨‍💼 Admin: http://localhost:${PORT}/admin`); console.log(`🔗 API: http://localhost:${PORT}/api`); }); module.exports = app;