The Wild

Falcon Static Files: Serve a Webpage Step by Step

· 6 min read · Birds

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

To serve a static webpage from a Falcon application, add a static route to your Falcon app with app.add_static_route('/static', '/path/to/your/files') and place your HTML, CSS, and JavaScript files in that directory. Falcon's built-in static route handler reads files from disk and returns them with the correct content type, so no extra web server is required for development or simple deployments. This guide walks through the setup, the directory layout, the code, and the mistakes that most often trip people up.

What You'll Need

  • Python 3.8 or newer installed and working from the command line.
  • Falcon installed in your environment: pip install falcon. For serving files you do not need any additional Falcon plugins.
  • A WSGI server for anything beyond local testing. Falcon ships with a simple development server, but production traffic should go through a server such as Gunicorn or Waitress.
  • A directory containing your static webpage: at minimum an index.html, plus any CSS, JavaScript, images, or fonts it references.

Step-by-Step

  1. Create a project folder and a static directory. Make a folder for your app and inside it a subfolder named static. Put your webpage files there. A typical layout looks like this: myapp/app.py, myapp/static/index.html, myapp/static/css/style.css, myapp/static/js/app.js. Keeping the static files in their own folder makes the route prefix easy to reason about and prevents accidental exposure of Python source files.
  2. Write the Falcon application. Create app.py and import Falcon. Instantiate the app, then register a static route. The first argument is the URL prefix clients will request, and the second is the absolute or relative path to the directory on disk. For example: import falcon; app = falcon.App(); app.add_static_route('/static', 'static'). The prefix and the folder name do not have to match, but matching them keeps things predictable.
  3. Decide how the root URL should behave. A static route at /static serves files under that prefix, so /static/index.html works but / does not automatically. If you want the homepage at the root, add a small resource class whose on_get method sets resp.content_type = 'text/html' and resp.text to the contents of your HTML file, or redirect the root to /static/index.html with resp.status = falcon.HTTP_302 and a Location header. Choose one approach and document it, because mixing them confuses both users and search engines.
  4. Run the app locally. Start the development server with falcon serve --bind http://localhost:8000 from the project directory, or use any WSGI runner. Open http://localhost:8000/static/index.html in a browser. You should see your page with its styles and scripts loading. If the HTML loads but the CSS does not, the problem is almost always a path in the HTML that does not match the route prefix.
  5. Check relative versus absolute asset paths. Inside index.html, reference assets using the same prefix you registered. If the route is /static, write <link rel="stylesheet" href="/static/css/style.css"> rather than href="css/style.css" unless the HTML itself is served from a matching subpath. Absolute paths starting with a slash are the least surprising and survive being served from nested URLs.
  6. Confirm content types are correct. Falcon guesses the media type from the file extension. Browsers refuse to apply a stylesheet served as text/plain, and modules fail if JavaScript is served with the wrong type. Open the browser developer tools, go to the Network tab, reload, and check the Content-Type response header for the HTML, CSS, and JS files. If a type is wrong, rename the file to the correct extension rather than fighting the handler.
  7. Harden the route for production. Static routes serve any file inside the directory, including dotfiles and backups, so keep secrets out of the static folder entirely. Configure your WSGI server to serve the static directory directly when possible, because it is faster and offloads work from Python. If Falcon must serve the files, place a reverse proxy in front and enable caching headers there. Also set a sensible cache lifetime for versioned assets and a short one for index.html so updates appear promptly.
  8. Deploy and verify. Run the app behind Gunicorn or Waitress, put a reverse proxy such as Nginx in front if you have one, and request the page over the public URL. Verify the HTML, one stylesheet, one script, and one image all return 200 responses. Test a missing file too: you should get a clean 404 rather than a stack trace or a directory listing.

Common Mistakes

The most frequent error is a prefix mismatch: the route is registered at /static but the HTML references /assets/style.css, so the browser gets 404s for every asset while the page itself loads unstyled. The second most common is pointing the static route at the project root instead of a dedicated folder, which can expose source files, configuration, and environment files to anyone who guesses the name. A third is forgetting that the root URL is not served automatically; people expect / to return index.html and are surprised by a 404. Finally, serving large files through the Python process under load causes slow responses and high memory use, which is why production setups usually let the reverse proxy or WSGI server handle static files.

Tips

Use a versioned or hashed filename for CSS and JavaScript bundles so you can set long cache lifetimes without users seeing stale files after a deploy. Keep the static directory flat enough that paths stay readable, and avoid spaces and uppercase letters in filenames because URL encoding and case-sensitive filesystems cause avoidable breakage. Add a custom 404 page by registering a handler for HTTPNotFound so missing assets do not look like a server crash. During development, disable browser caching in the developer tools; otherwise you may spend time debugging a file the browser never re-requested. If you serve the page from a subpath, test every link and asset path from that subpath, not just from the root.

Troubleshooting

If the page returns 404, confirm the route prefix and the requested URL match exactly, including trailing slashes, and confirm the file exists at the path you think it does by listing the directory from the same working directory the app runs in. If assets return 404 but the HTML loads, inspect the Network tab for the exact URLs requested and compare them with the route prefix. If the browser downloads the HTML instead of rendering it, the content type is wrong; check the file extension and the response header. If changes do not appear, clear the browser cache or hard-reload, and check whether a proxy is caching responses. If you get a 500 error, read the server traceback: a common cause is a relative static path resolved against the wrong working directory, which is fixed by using an absolute path built from os.path.dirname(__file__). If directory listings appear, stop serving the folder and restrict the route to known files, or move sensitive files out of it.

Frequently Asked Questions

Can Falcon serve a full website without Nginx?

Yes for development and light traffic. For production, a reverse proxy or the WSGI server itself is the better place to serve static files because it is faster and keeps Python workers free for application logic.

Why does my CSS return a 404 when the HTML works?

The asset URL in the HTML does not match the registered static route prefix. Check the Network tab for the requested path and align it with the prefix you passed to add_static_route.

How do I serve index.html at the root URL?

Add a resource whose on_get returns the HTML content with text/html, or redirect / to the static file URL. Falcon does not map the root to a directory index automatically.

Does Falcon set cache headers for static files?

It does not add long-lived caching by default. Set cache headers in your reverse proxy or WSGI server, and use hashed filenames for assets you want cached aggressively.

Can I serve files from outside the project directory?

Yes, pass an absolute path to add_static_route. Keep that directory free of secrets, because every file inside it becomes reachable by URL.

What happens if a requested static file does not exist?

Falcon returns a 404 response. You can customise the body by registering an error handler for HTTPNotFound if you want a branded error page.