Homework & Coding Practice

Homework/Practice Coding Assignment (20-min)

Task 1: Serve Static HTML Files for /home and /contact

Step-by-Step Procedure:

  1. Step 1: Create a folder named public in the project directory.

  2. Step 2: Add home.html and contact.html to the public folder:

    • home.html:

      html코드 복사<!DOCTYPE html>
      <html>
      <head><title>Home</title></head>
      <body>
          <h1>Welcome to the Home Page</h1>
      </body>
      </html>
    • contact.html:

      html코드 복사<!DOCTYPE html>
      <html>
      <head><title>Contact</title></head>
      <body>
          <h1>Contact Us</h1>
      </body>
      </html>
  3. Step 3: Serve the files using express.static:

    javascript코드 복사app.use(express.static('public'));
  4. Step 4: Test the Application:

    • Open a browser and navigate to:

      • http://localhost:3000/home.html

      • http://localhost:3000/contact.html


Task 2: Add Middleware to Log Request Time and Method

Step-by-Step Procedure:

  1. Step 1: Update the logging middleware in app.js:

    javascript코드 복사app.use((req, res, next) => {
        const time = new Date().toISOString();
        console.log(`[${time}] ${req.method} ${req.url}`);
        next();
    });
  2. Step 2: Test by making requests to different routes:

    • Access /home.html and /contact.html in the browser.

    • Observe the logs in the terminal.

Expected Outcomes

  1. Students learn to use middleware for logging and validation.

  2. Students understand how to serve static files in an Express application.

  3. Homework reinforces middleware concepts and static file serving in real-world applications.

Last updated