107 lines
4.1 KiB
JavaScript
107 lines
4.1 KiB
JavaScript
const express = require('express');
|
|
const path = require('path');
|
|
const fs = require('fs');
|
|
|
|
const app = express();
|
|
const PORT = process.env.PORT || 8888;
|
|
|
|
// Aggressive caching middleware for static assets
|
|
app.use((req, res, next) => {
|
|
// Set cache headers based on file type
|
|
const ext = path.extname(req.path).toLowerCase();
|
|
|
|
// Images - cache for 1 year
|
|
if (['.jpg', '.jpeg', '.png', '.gif', '.webp', '.svg', '.ico'].includes(ext)) {
|
|
res.setHeader('Cache-Control', 'public, max-age=31536000, immutable');
|
|
}
|
|
// CSS/JS - cache for 1 month with revalidation
|
|
else if (['.css', '.js'].includes(ext) && !req.path.includes('animations.js')) {
|
|
res.setHeader('Cache-Control', 'public, max-age=2592000, must-revalidate');
|
|
}
|
|
// HTML - cache for 1 hour with revalidation
|
|
else if (ext === '.html' || req.path === '/') {
|
|
res.setHeader('Cache-Control', 'public, max-age=3600, must-revalidate');
|
|
res.setHeader('X-Content-Type-Options', 'nosniff');
|
|
}
|
|
// Fonts - cache for 1 year
|
|
else if (['.woff', '.woff2', '.ttf', '.eot'].includes(ext)) {
|
|
res.setHeader('Cache-Control', 'public, max-age=31536000, immutable');
|
|
}
|
|
|
|
// Add performance headers
|
|
res.setHeader('X-Frame-Options', 'SAMEORIGIN');
|
|
res.setHeader('X-XSS-Protection', '1; mode=block');
|
|
|
|
next();
|
|
});
|
|
|
|
// Enable compression (install compression middleware: npm install compression)
|
|
// const compression = require('compression');
|
|
// app.use(compression());
|
|
|
|
// Serve static files from the current directory
|
|
app.use(express.static(__dirname, {
|
|
etag: true,
|
|
lastModified: true,
|
|
setHeaders: (res, filePath) => {
|
|
// Additional headers for specific files
|
|
if (filePath.endsWith('.html')) {
|
|
res.setHeader('X-Powered-By', 'Odoo Expertos');
|
|
}
|
|
}
|
|
}));
|
|
|
|
// Special handling for root route
|
|
app.get('/', (req, res) => {
|
|
res.sendFile(path.join(__dirname, 'index.html'));
|
|
});
|
|
|
|
// Handle directory routes - serve the index.html file or directory listing
|
|
app.get('*', (req, res) => {
|
|
const requestedPath = path.join(__dirname, req.path);
|
|
|
|
// First check if the exact file exists (for static files)
|
|
if (fs.existsSync(requestedPath) && fs.statSync(requestedPath).isFile()) {
|
|
res.sendFile(requestedPath);
|
|
return;
|
|
}
|
|
|
|
// Check if it's a directory
|
|
if (fs.existsSync(requestedPath) && fs.statSync(requestedPath).isDirectory()) {
|
|
const indexPath = path.join(requestedPath, 'index.html');
|
|
if (fs.existsSync(indexPath)) {
|
|
res.sendFile(indexPath);
|
|
} else {
|
|
res.status(404).send('Directory index not found');
|
|
}
|
|
} else {
|
|
// Try to serve 404.html if it exists
|
|
const notFoundPath = path.join(__dirname, '404.html');
|
|
if (fs.existsSync(notFoundPath)) {
|
|
res.status(404).sendFile(notFoundPath);
|
|
} else {
|
|
res.status(404).send('Page not found');
|
|
}
|
|
}
|
|
});
|
|
|
|
app.listen(PORT, () => {
|
|
console.log(`
|
|
╔════════════════════════════════════════════════════════════╗
|
|
║ ║
|
|
║ 🚀 Odoo Expertos OPTIMIZED Server Running! ║
|
|
║ ║
|
|
║ Local URL: http://localhost:${PORT} ║
|
|
║ ║
|
|
║ ✅ Caching enabled for static assets ║
|
|
║ ✅ Performance headers active ║
|
|
║ ║
|
|
║ Press Ctrl+C to stop the server ║
|
|
║ ║
|
|
╚════════════════════════════════════════════════════════════╝
|
|
`);
|
|
|
|
// Open browser automatically
|
|
const opener = require('opener');
|
|
opener(`http://localhost:${PORT}`);
|
|
}); |