> ## Documentation Index
> Fetch the complete documentation index at: https://docs.lightning-go.cn/llms.txt
> Use this file to discover all available pages before exploring further.

# Static Files

> Serve CSS, JavaScript, images, and other static assets from a directory using app.Static().

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

```go theme={null}
app.Static("./public", "/assets")
```

This serves every file inside `./public` under the `/assets` URL prefix:

| File on disk            | URL                    |
| ----------------------- | ---------------------- |
| `./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.

```go theme={null}
// Relative: resolved from the executable's directory
app.Static("./public", "/static")

// Absolute: used directly
app.Static("/var/www/myapp/public", "/static")
```

<Note>
  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`.
</Note>

## Complete Example

**Directory structure:**

```
myapp/
├── main.go
└── public/
    ├── logo.png
    ├── css/
    │   └── style.css
    └── js/
        └── app.js
```

**`main.go`:**

```go theme={null}
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:

```bash theme={null}
curl http://localhost:8080/assets/logo.png
curl http://localhost:8080/assets/css/style.css
curl http://localhost:8080/assets/js/app.js
```

<Tip>
  You can register multiple static directories with different prefixes in the same application:

  ```go theme={null}
  app.Static("./public", "/static")
  app.Static("./uploads", "/media")
  ```
</Tip>
