# Middleware (`http/middleware`)

Fiber supports synchronous middleware handlers and separate async middleware
lanes for the routes that expose them. Middleware registration occurs during
application setup; consult [Fiber](fiber.md) for the lane-specific rules.

This modular architecture allows you to easily inject utilities right into the HTTP lifecycle:

The examples require the `http` standard-library modules to be resolvable by
your compiler. In a source checkout, setting `VEX_STDLIB_PATH` to the
checkout's `lib/std` directory is one way to select them. Availability and
resolution in a downloadable release must be checked separately.

```vex
import { logger, cors, CorsConfig } from "http/middleware";
```

## Logger

When installed first, `logger` wraps subsequent handlers and logs method, path,
response status and elapsed time. The implementation does not add a timestamp.

```vex
app.use(logger);

// Output:
// Example shape: [GET] /api/users → 200 (1ms)
```

## CORS (Cross-Origin Resource Sharing)

`cors` adds allow-origin, methods, headers and max-age response headers. It
answers `OPTIONS` with status 204. CORS is a browser policy and does not replace
authentication or authorization.

```vex
app.use(cors); // Allows any origin
// Or instead, configure a specific origin:
app.use(CorsConfig.new("https://example.com").build());
```

## Static files

Static serving is deliberately a route API, so it shares Fiber's frozen
router and request lifecycle rather than adding a second middleware path.

```vex
app.static("/static", "./public");
```

The implementation checks parent traversal, canonicalizes the configured root
and candidate, and rejects candidates outside the root at check time. It then
opens the canonical path in a separate operation, so this check does not prove
confinement if an attacker can change path components concurrently. Keep the
served tree writable only by trusted actors and validate the threat model for
your deployment. The HTTP/1 implementation queues an opened file for bounded
descriptor-based streaming; it is not a general asset-pipeline replacement.
HTTP/2 static-route behavior requires separate validation on the target.
