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}/`);
});

Các câu lạc bộ
Việt Nam
Việt Nam 63.811 thành viên
Twelve Kizuki
Twelve Kizuki 60 thành viên
hashira member
hashira member 23 thành viên
Vietnamchessclub
Vietnamchessclub 11 thành viên
Vietnam Study Chess
Vietnam Study Chess 344 thành viên