0
0
Flaskframework~20 mins

Serving JavaScript files in Flask - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Flask Static Files Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output when serving a JavaScript file with Flask static folder?
Given this Flask app setup, what will the browser receive when requesting /static/app.js?
Flask
from flask import Flask
app = Flask(__name__)

# Assume 'static/app.js' contains: console.log('Hello from JS');

if __name__ == '__main__':
    app.run()
AFlask returns a 404 error because static files are not served by default.
BThe browser receives the JavaScript file content: console.log('Hello from JS');
CThe browser receives an HTML page with the text 'Hello from JS'.
DFlask returns a 500 internal server error due to missing route.
Attempts:
2 left
💡 Hint
Flask automatically serves files from the 'static' folder at the '/static' URL path.
📝 Syntax
intermediate
2:00remaining
Which Flask route correctly serves a JavaScript file from a custom folder?
You want to serve JavaScript files from a folder named 'js' inside your project root. Which route code correctly serves the file filename?
A
@app.route('/js/<filename>')
def serve_js(filename):
    return redirect('/static/js/' + filename)
B
@app.route('/js/<filename>')
def serve_js(filename):
    return open('js/' + filename).read()
C
@app.route('/js/<filename>')
def serve_js(filename):
    return send_file('js/' + filename)
D
from flask import send_from_directory
@app.route('/js/<path:filename>')
def serve_js(filename):
    return send_from_directory('js', filename)
Attempts:
2 left
💡 Hint
Use Flask's send_from_directory to serve files from a folder.
🔧 Debug
advanced
2:00remaining
Why does this Flask app fail to serve JavaScript files from 'static' folder?
Consider this Flask app code snippet: from flask import Flask app = Flask(__name__, static_folder='assets') # JavaScript file is located at 'assets/app.js' if __name__ == '__main__': app.run() When requesting '/static/app.js', the browser gets a 404 error. Why?
ABecause the static_folder is set to 'assets', Flask serves static files at '/assets', not '/static'.
BBecause Flask requires a route to serve static files explicitly when static_folder is set.
CBecause the JavaScript file must be inside a folder named 'static' for Flask to serve it.
DBecause the app.run() call is missing debug=True to enable static serving.
Attempts:
2 left
💡 Hint
Changing static_folder changes the URL path prefix for static files.
state_output
advanced
2:00remaining
What is the Content-Type header when Flask serves a JavaScript file?
If Flask serves a file named 'script.js' from the static folder, what is the Content-Type header sent to the browser?
Aapplication/javascript
Btext/html
Ctext/plain
Dapplication/json
Attempts:
2 left
💡 Hint
Browsers expect JavaScript files to have a specific MIME type.
🧠 Conceptual
expert
3:00remaining
How to serve versioned JavaScript files to avoid browser caching in Flask?
You want to serve JavaScript files with a version query parameter like /static/app.js?v=123 to force browsers to reload updated files. Which approach is best in Flask?
ARename the JavaScript file on disk every time you update it and serve it normally.
BSet a custom route to serve JavaScript files and ignore query parameters to always reload.
CUse Flask's <code>url_for('static', filename='app.js', v='123')</code> to generate URLs with version query parameters.
DDisable browser caching globally in Flask by setting response headers for all static files.
Attempts:
2 left
💡 Hint
Flask's url_for can add query parameters to static file URLs.