DevJourney

HTML/html.nodejs.dj.upayan.dev/web/server.js

const express = require('express');
const fs = require('fs');
const path = require('path');
const app = express();
const port = 3000;

app.use(express.static(path.join(__dirname, 'public')));

app.get('/', (req, res) => {
    res.sendFile(path.join(__dirname, 'routes', 'index.html'));
});

app.get('/:page', (req, res) => {
    const page = req.params.page;
    const filePath = path.join(__dirname, 'routes', `${page}.html`);
    
    fs.access(filePath, fs.constants.F_OK, (err) => {
        if (err) {
            res.status(404).send('Page not found');
        } else {
            res.sendFile(filePath);
        }
    });
});

app.listen(port, () => {
    console.log(`Web server running at http://localhost:${port}`);
});
View on GitHub