Homework & Coding Practice
Homework/Practice Coding Assignment (20-min)
Task 1: Serve Static HTML Files for /home and /contact
Step-by-Step Procedure:
Step 1: Create a folder named
publicin the project directory.Step 2: Add
home.htmlandcontact.htmlto thepublicfolder: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>
Step 3: Serve the files using
express.static:javascript코드 복사app.use(express.static('public'));Step 4: Test the Application:
Open a browser and navigate to:
http://localhost:3000/home.htmlhttp://localhost:3000/contact.html
Task 2: Add Middleware to Log Request Time and Method
Step-by-Step Procedure:
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(); });Step 2: Test by making requests to different routes:
Access
/home.htmland/contact.htmlin the browser.Observe the logs in the terminal.
Expected Outcomes
Students learn to use middleware for logging and validation.
Students understand how to serve static files in an Express application.
Homework reinforces middleware concepts and static file serving in real-world applications.
Last updated