Bird
0
0

Which of the following is the correct way to mount a directory named static to serve files at the URL path /assets in FastAPI?

easy📝 Syntax Q3 of 15
FastAPI - File Handling
Which of the following is the correct way to mount a directory named static to serve files at the URL path /assets in FastAPI?
Aapp.mount('/static', StaticFiles(directory='assets'), name='static')
Bapp.mount('static', StaticFiles(directory='/assets'), name='static')
Capp.mount('/assets', StaticFiles(directory='static'), name='assets')
Dapp.mount('/assets', StaticFiles('static'))
Step-by-Step Solution
Solution:
  1. Step 1: Understand mount syntax

    The mount method takes the URL path first, then a StaticFiles instance with the directory parameter.
  2. Step 2: Match directory and URL path

    To serve files from the 'static' folder at '/assets', the URL path is '/assets' and directory is 'static'.
  3. Step 3: Name parameter

    Providing a name is optional but recommended for routing purposes.
  4. Final Answer:

    app.mount('/assets', StaticFiles(directory='static'), name='assets') -> Option C
  5. Quick Check:

    URL path first, then StaticFiles with directory [OK]
Quick Trick: Mount URL path first, then StaticFiles(directory=...) [OK]
Common Mistakes:
MISTAKES
  • Swapping URL path and directory arguments
  • Omitting the directory keyword argument
  • Using incorrect URL path or directory names

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More FastAPI Quizzes