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 63,487 सदस्य
hashira member
hashira member 23 सदस्य
Twelve Kizuki
Twelve Kizuki 60 सदस्य
Blox Fruits fan club
Blox Fruits fan club 65 सदस्य
Cờ Vua Thông Minh
Cờ Vua Thông Minh 4,642 सदस्य