Bird
0
0

How can you serve static files with caching headers set to 1 day in Express?

hard📝 Application Q9 of 15
Node.js - HTTP Module
How can you serve static files with caching headers set to 1 day in Express?
Aapp.use(express.static('public', { maxAge: '1d' }));
Bapp.use(express.static('public', { maxAge: 86400000 }));
Capp.use(express.static('public', { cache: '1 day' }));
Dapp.use(express.static('public', { cacheControl: true, maxAge: 86400 }));
Step-by-Step Solution
Solution:
  1. Step 1: Understand maxAge option for caching

    The maxAge option expects milliseconds to set cache duration.
  2. Step 2: Convert 1 day to milliseconds

    1 day = 24 * 60 * 60 * 1000 = 86400000 milliseconds.
  3. Step 3: Check option syntax

    app.use(express.static('public', { maxAge: 86400000 })); correctly uses { maxAge: 86400000 } without extra invalid keys.
  4. Final Answer:

    app.use(express.static('public', { maxAge: 86400000 })); -> Option B
  5. Quick Check:

    maxAge in ms sets cache duration [OK]
Quick Trick: Use maxAge in milliseconds to set cache duration [OK]
Common Mistakes:
  • Using string instead of ms for maxAge
  • Adding invalid options
  • Confusing cacheControl with maxAge

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Node.js Quizzes