How do you serve static JavaScript files in a Flask application?
Place your JavaScript files in the 'static' folder of your Flask project. Then, use the url_for('static', filename='yourfile.js') function in your HTML to link to the JavaScript file.
Click to reveal answer
beginner
What is the default folder name Flask uses to serve static files like JavaScript?
Flask uses a folder named static by default to serve static files such as JavaScript, CSS, and images.
Click to reveal answer
beginner
Why should JavaScript files be placed in the 'static' folder in Flask?
Because Flask automatically serves files from the 'static' folder at the URL path '/static/'. This makes it easy to link JavaScript files in your HTML without extra configuration.
Click to reveal answer
beginner
How do you include a JavaScript file named 'app.js' located in the static folder in your Flask HTML template?
Use the script tag with Flask's url_for function: <script src="{{ url_for('static', filename='app.js') }}"></script>
Click to reveal answer
intermediate
Can you serve JavaScript files from a folder other than 'static' in Flask by default?
No, Flask serves static files only from the 'static' folder by default. To serve files from another folder, you must configure Flask explicitly.
Click to reveal answer
Where should you place JavaScript files in a Flask project to serve them easily?
AIn the root folder
BIn the 'templates' folder
CIn the 'js' folder
DIn the 'static' folder
✗ Incorrect
Flask serves static files like JavaScript from the 'static' folder by default.
How do you reference a JavaScript file named 'main.js' in a Flask HTML template?
But the browser shows the JavaScript code as plain text instead of running it. What is the problem?
medium
A. The response is missing the correct Content-Type header for JavaScript
B. The file path 'static/app.js' is incorrect
C. You cannot serve JavaScript files manually in Flask
D. The route path should be '/static/app.js' instead of '/js/app.js'
Solution
Step 1: Check how Flask serves static files
Flask sets the Content-Type header to 'application/javascript' automatically for static files.
Step 2: Identify missing Content-Type in manual response
Returning raw file content without setting Content-Type causes browser to treat it as plain text.
Final Answer:
The response is missing the correct Content-Type header for JavaScript -> Option A
Quick Check:
Set Content-Type to application/javascript for JS files [OK]
Hint: Always set Content-Type header when serving JS manually [OK]
Common Mistakes:
Not setting Content-Type header
Assuming Flask auto-sets headers in manual routes
Using wrong file path without checking
5. You want to serve multiple JavaScript files from a custom folder named assets/js instead of the default static folder. Which Flask code snippet correctly serves these files at URL path /js/<filename>?
hard
A. @app.route('/js/<filename>')
def serve_js(filename):
return open(f'assets/js/{filename}').read()
B. from flask import send_file
@app.route('/js/<filename>')
def serve_js(filename):
return send_file(f'assets/js/{filename}')
C. from flask import send_from_directory
@app.route('/js/<filename>')
def serve_js(filename):
return send_from_directory('assets/js', filename)
D. @app.route('/js/<filename>')
def serve_js(filename):
return redirect(f'/static/{filename}')
Solution
Step 1: Use send_from_directory to serve files from custom folder
Flask's send_from_directory safely serves files from any folder with correct headers.
Step 2: Map URL path to custom folder
The route /js/<filename> calls send_from_directory('assets/js', filename) to serve files from assets/js.
Final Answer:
Use send_from_directory with assets/js folder -> Option C