const express = require('express'); const path = require('path'); const app = express(); const PORT = process.env.PORT || 3000; // Serve static files from the 'public' directory app.use(express.static(path.join(__dirname, 'public'))); // Custom middleware to handle URLs without .html for specific routes app.use((req, res, next) => { // Extract the path without any query parameters const urlPath = req.path.split('?')[0]; // Define routes that should render HTML files without .html extension const htmlRoutes = ['/about', '/list', '/gallery']; // Check if the requested path is in the htmlRoutes array if (htmlRoutes.includes(urlPath)) { // Append .html to the path and continue req.url += '.html'; } // Continue to the next middleware next(); }); // Route to serve the index.html file app.get('/', (req, res) => { res.sendFile(path.join(__dirname, 'public', 'index.html')); }); // Routes to serve the HTML files without .html extension app.get('/about.html', (req, res) => { res.sendFile(path.join(__dirname, 'public', 'about.html')); }); app.get('/list.html', (req, res) => { res.sendFile(path.join(__dirname, 'public', 'list.html')); }); app.get('/gallery.html', (req, res) => { res.sendFile(path.join(__dirname, 'public', 'gallery.html')); }); // Serve articles without .html extension app.get('/articles/:articleName', (req, res) => { const articleName = req.params.articleName; res.sendFile(path.join(__dirname, 'public/articles', `${articleName}.html`)); }); // Error handling app.use((err, req, res, next) => { console.error(err.stack); res.status(500).send('Something broke!'); }); // Start the server app.listen(PORT, (err) => { if (err) { console.error('Error starting the server:', err); process.exit(1); } console.log(`Server is running on http://localhost:${PORT}`); });