PozoAppUptoWebinar/server/server.js

44 lines
1.2 KiB
JavaScript

import express from 'express';
import path from 'path';
import { fileURLToPath } from 'url';
import seoMiddleware from './seo-middleware.js';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const app = express();
const PORT = process.env.PORT || process.env.IISNODE_PORT || 3000;
// Configure MIME types for JSX files
app.use((req, res, next) => {
if (req.path.endsWith('.jsx')) {
res.setHeader('Content-Type', 'application/javascript');
}
next();
});
// Serve static files FIRST (CSS, JS, images, favicon)
app.use('/assets', express.static(path.join(__dirname, '../dist/assets')));
app.use('/src', express.static(path.join(__dirname, '../src')));
app.use(express.static(path.join(__dirname, '../dist')));
// Redirect /home to root
app.use((req, res, next) => {
if (req.path.startsWith('/home')) {
const newPath = req.path.replace('/home', '') || '/';
return res.redirect(301, newPath);
}
next();
});
// Apply SEO middleware for HTML pages
app.get('*', seoMiddleware);
app.head('*', seoMiddleware);
app.listen(PORT, () => {
console.log(`Server running on port ${PORT}`);
console.log('Dynamic SEO enabled!');
});
export default app;