Bird
0
0

You want to manually route requests to serve static files like '/index.html' or '/style.css'. Which approach correctly reads and serves these files using Node.js core modules?

hard📝 Application Q8 of 15
Node.js - HTTP Module
You want to manually route requests to serve static files like '/index.html' or '/style.css'. Which approach correctly reads and serves these files using Node.js core modules?
AUse <code>res.writeFile</code> to write the file directly to response
BUse <code>http.get</code> to fetch the file from disk
CUse <code>fs.readFile</code> to read the file and send its content in the response
DUse <code>req.readFile</code> to read the file from request
Step-by-Step Solution
Solution:
  1. Step 1: Identify correct module and method

    The fs module's readFile method reads files from disk asynchronously.
  2. Step 2: Send file content in response

    After reading, the content is sent with res.write() and res.end().
  3. Final Answer:

    Use fs.readFile to read the file and send its content in the response -> Option C
  4. Quick Check:

    Serve static files = fs.readFile + res.write [OK]
Quick Trick: Use fs.readFile to serve static files [OK]
Common Mistakes:
  • Using non-existent res.writeFile
  • Trying to read files from request object
  • Using http.get for local files

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Node.js Quizzes