Static
Serves files directly from a directory inside your application's src directory. In addition to serving individual files, it supports index.html files as directory index pages, allowing static directories to behave like routes.
Installation
embr add mayari/static
embr iUsage
local Mayari = require("mayari")
local static = require("static")
local app = Mayari.new(8080, { static("public") })The directory passed to static() is resolved relative to the project's src directory.
For example:
my-project/
├── src/
│ ├── app/
│ │ └── ...
│ ├── public/
│ │ ├── index.html
│ │ ├── styles.css
│ │ └── images/
│ │ └── logo.png
│ └── index.luau
└── ...With:
static("public")the files are available as:
/ → public/index.html
/styles.css → public/styles.css
/images/logo.png → public/images/logo.pngDirectory Index
index.html has special behavior.
When a directory contains an index.html, the file is served when the directory itself is requested.
public/
├── index.html
├── about/
│ └── index.html
└── docs/
├── index.html
└── guide.htmlThis produces the following routes:
| Request | File |
|---|---|
/ | public/index.html |
/about | public/about/index.html |
/docs | public/docs/index.html |
/docs/guide.html | public/docs/guide.html |
This makes it possible to create static pages using a directory-based structure without defining a route.luau for every page.
Static Files
Files that are not index.html are served using their filesystem path.
public/
├── favicon.ico
├── styles.css
├── script.js
└── images/
├── logo.png
└── banner.webpThey can be accessed through:
/favicon.ico
/styles.css
/script.js
/images/logo.png
/images/banner.webpContent Types
The middleware automatically assigns a Content-Type based on the file extension.
Common supported types include:
| Extension | Content-Type |
|---|---|
.html | text/html |
.css | text/css |
.js | application/javascript |
.json | application/json |
.txt | text/plain |
.ico | image/x-icon |
.png | image/png |
.jpg / .jpeg | image/jpeg |
.gif | image/gif |
.svg | image/svg+xml |
.webp | image/webp |
.woff | font/woff |
.woff2 | font/woff2 |
.ttf | font/ttf |
.wasm | application/wasm |
.mp4 | video/mp4 |
.mkv | video/x-matroska |
Files with an unrecognized extension use:
application/octet-streamHTML, CSS, and JavaScript files use Mayari's corresponding response helpers.