TranMinhTriNewbie111

// Import the necessary modules
const http = require('http');
const fs = require('fs');
const path = require('path');

// Define the server's port number
const PORT = 3000;

// Create an HTTP server
const server = http.createServer((req, res) => {
  console.log(`Request for ${req.url} received.`);

  // Serve different pages based on the URL
  if (req.url === '/') {
    // Serve the home page
    fs.readFile(path.join(__dirname, 'index.html'), 'utf8', (err, data) => {
      if (err) {
        res.statusCode = 500;
        res.end('Error loading the homepage.');
        return;
      }
      res.statusCode = 200;
      res.setHeader('Content-Type', 'text/html');
      res.end(data);
    });
  } else if (req.url === '/about') {
    // Serve the about page
    res.statusCode = 200;
    res.setHeader('Content-Type', 'text/html');
    res.end('<h1>About Us</h1><p>We are a sample server-side JavaScript app.</p>');
  } else {
    // Handle unknown routes
    res.statusCode = 404;
    res.setHeader('Content-Type', 'text/html');
    res.end('<h1>404 Not Found</h1><p>The page you are looking for does not exist.</p>');
  }
});

// Start the server and listen on the specified port
server.listen(PORT, () => {
  console.log(`Server running at http://localhost:${PORT}/`);
});

ক্লাব
Việt Nam
Việt Nam ৬৩,৭০৯ জন সদস্য
Vietnamcommunity
Vietnamcommunity ৪২ জন সদস্য
Kaiokand Alliance
Kaiokand Alliance ১৮ জন সদস্য
hashira member
hashira member ২৩ জন সদস্য
Twelve Kizuki
Twelve Kizuki ৬০ জন সদস্য