xk/backend/server.js

75 lines
2.0 KiB
JavaScript
Raw Normal View History

2026-02-02 20:51:52 +08:00
const express = require('express');
const session = require('express-session');
const cors = require('cors');
const path = require('path');
require('dotenv').config();
const app = express();
const PORT = process.env.PORT || 3000;
// Middleware
app.use(cors({
origin: true,
credentials: true
}));
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
// Session configuration
app.use(session({
secret: process.env.SESSION_SECRET || 'xuking_origin_secret_key',
resave: false,
saveUninitialized: false,
cookie: {
secure: false, // set to true if using https
httpOnly: true,
maxAge: 24 * 60 * 60 * 1000 // 24 hours
}
}));
// Serve static files from parent directory
app.use(express.static(path.join(__dirname, '..')));
app.use('/admin', express.static(path.join(__dirname, 'public')));
// API Routes
const authRoutes = require('./routes/auth');
const partnerRoutes = require('./routes/partners');
const configRoutes = require('./routes/config');
const videoRoutes = require('./routes/videos');
app.use('/api/auth', authRoutes);
app.use('/api/partners', partnerRoutes);
app.use('/api/config', configRoutes);
app.use('/api/videos', videoRoutes);
// Admin panel routes
app.get('/admin', (req, res) => {
res.sendFile(path.join(__dirname, 'public', 'index.html'));
});
app.get('/admin/*', (req, res) => {
res.sendFile(path.join(__dirname, 'public', 'index.html'));
});
// Root redirect
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({
success: false,
message: 'Something went wrong!',
error: process.env.NODE_ENV === 'development' ? err.message : undefined
});
});
// Start server
app.listen(PORT, () => {
console.log(`\n🚀 Server running on http://localhost:${PORT}`);
console.log(`📊 Admin panel: http://localhost:${PORT}/admin`);
console.log(`🌐 Website: http://localhost:${PORT}\n`);
});