Skip to main content
Use app.Static(root, prefix) to map a URL prefix to a directory on disk. Lightning registers a wildcard route under that prefix and streams matching files directly to the client.

Basic Usage

app.Static("./public", "/assets")
This serves every file inside ./public under the /assets URL prefix:
File on diskURL
./public/logo.png/assets/logo.png
./public/css/main.css/assets/css/main.css
./public/js/app.js/assets/js/app.js
If a requested file does not exist, Lightning responds with 404 Not Found.

Absolute vs. Relative Paths

  • Relative path — resolved relative to the directory of the running executable, not the working directory. For example, "./public" is joined with the path to the compiled binary.
  • Absolute path — used as-is. Pass an absolute path when you want deterministic resolution regardless of where the binary lives.
// Relative: resolved from the executable's directory
app.Static("./public", "/static")

// Absolute: used directly
app.Static("/var/www/myapp/public", "/static")
During development you typically run go run main.go from your project root, so ./public resolves relative to the compiled temp binary’s directory, which may not be your project root. If files are not found, switch to an absolute path or embed assets with //go:embed.

Complete Example

Directory structure:
myapp/
├── main.go
└── public/
    ├── logo.png
    ├── css/
    │   └── style.css
    └── js/
        └── app.js
main.go:
package main

import (
    "github.com/go-labx/lightning"
)

func main() {
    app := lightning.DefaultApp()

    // Serve ./public/* at /assets/*
    app.Static("./public", "/assets")

    app.Get("/", func(ctx *lightning.Context) {
        ctx.Text(lightning.StatusOK, "Hello from Lightning!")
    })

    app.Run(":8080")
}
With the server running, the following URLs serve the corresponding files:
curl http://localhost:8080/assets/logo.png
curl http://localhost:8080/assets/css/style.css
curl http://localhost:8080/assets/js/app.js
You can register multiple static directories with different prefixes in the same application:
app.Static("./public", "/static")
app.Static("./uploads", "/media")