PozoAppOptimandSEO/dist1 (2)/server.js

41 lines
1.4 KiB
JavaScript

import express from 'express';
import path from 'path';
import { fileURLToPath } from 'url';
import seoMiddleware from './server/seo-middleware.js';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const app = express();
const PORT = process.env.PORT || 3000;
// CRITICAL: Apply SEO middleware FIRST (before static files)
// This ensures HTML requests go through SEO middleware
app.use(seoMiddleware);
// Serve static files - BUT skip HTML files (middleware handles them)
app.use((req, res, next) => {
// If it's an HTML file or route without extension, skip static middleware
if (req.path.endsWith('.html') || (!req.path.match(/\.[a-z]{2,4}$/i) && req.method === 'GET' && !req.path.startsWith('/assets'))) {
return next(); // Let SEO middleware handle it
}
// Serve CSS, JS, images, fonts, etc.
express.static(__dirname, { index: false })(req, res, next);
});
app.use('/home', (req, res, next) => {
// If it's an HTML file or route without extension, skip static middleware
if (req.path.endsWith('.html') || (!req.path.match(/\.[a-z]{2,4}$/i) && req.method === 'GET' && !req.path.startsWith('/assets'))) {
return next(); // Let SEO middleware handle it
}
// Serve CSS, JS, images, fonts, etc.
express.static(__dirname, { index: false })(req, res, next);
});
app.listen(PORT, () => {
console.log(`Server running on port ${PORT}`);
console.log('Dynamic SEO enabled!');
});
export default app;