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 ৬৪,০১০ জন সদস্য
Vietnam Study Chess
Vietnam Study Chess ৩৫৪ জন সদস্য
Kaiokand Alliance
Kaiokand Alliance ২০ জন সদস্য
Vietnamchessclub
Vietnamchessclub ১১ জন সদস্য
Vietnamcommunity
Vietnamcommunity ৪১ জন সদস্য