Skip to content

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 ​

bash
embr add mayari/static
embr i

Usage ​

luau
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:

bash
my-project/
├── src/
│   ├── app/
│   │   └── ...
│   ├── public/
│   │   ├── index.html
│   │   ├── styles.css
│   │   └── images/
│   │       └── logo.png
│   └── index.luau
└── ...

With:

luau
static("public")

the files are available as:

text
/                  → public/index.html
/styles.css        → public/styles.css
/images/logo.png   → public/images/logo.png

Directory Index ​

index.html has special behavior.

When a directory contains an index.html, the file is served when the directory itself is requested.

text
public/
├── index.html
├── about/
│   └── index.html
└── docs/
    ├── index.html
    └── guide.html

This produces the following routes:

RequestFile
/public/index.html
/aboutpublic/about/index.html
/docspublic/docs/index.html
/docs/guide.htmlpublic/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.

text
public/
├── favicon.ico
├── styles.css
├── script.js
└── images/
    ├── logo.png
    └── banner.webp

They can be accessed through:

text
/favicon.ico
/styles.css
/script.js
/images/logo.png
/images/banner.webp

Content Types ​

The middleware automatically assigns a Content-Type based on the file extension.

Common supported types include:

ExtensionContent-Type
.htmltext/html
.csstext/css
.jsapplication/javascript
.jsonapplication/json
.txttext/plain
.icoimage/x-icon
.pngimage/png
.jpg / .jpegimage/jpeg
.gifimage/gif
.svgimage/svg+xml
.webpimage/webp
.wofffont/woff
.woff2font/woff2
.ttffont/ttf
.wasmapplication/wasm
.mp4video/mp4
.mkvvideo/x-matroska

Files with an unrecognized extension use:

text
application/octet-stream

HTML, CSS, and JavaScript files use Mayari's corresponding response helpers.