|
ProtoCore v1.0.16
Deterministic, zero-heap network stack for embedded targets
|
Layer: L7 Application ยท Build flags: none (PROTOCORE_ENABLE_FILE_SERVING is on by default)
serve_static(url_prefix, fs, fs_root) mounts a filesystem subtree at a URL prefix and serves files from it with content types inferred from the extension - the one-call way to ship a web UI (HTML/CSS/JS) from flash storage.
Mapping a URL tree to a directory. A request for / maps to /www/index.html, /app.js to /www/app.js, and so on:
set_cache_control() adds a Cache-Control header to served files so browsers cache them; combined with ETag the eventual revalidation is a cheap 304. Files are streamed in FILE_CHUNK_SIZE pieces, so even large assets use constant memory.
You must put your assets under a data/www/ folder and upload the LittleFS image ("Upload Filesystem Image" in PlatformIO / Arduino) before running - the sketch mounts LittleFS with begin(true) (format-on-fail).
Upload a filesystem image containing www/index.html, flash, then browse to http://<ip>/.
The complete sketch (FileServing.ino), reproduced verbatim with added explanatory comments: