Skip to content

Static Files

Serve static files (CSS, JS, images) from a directory.

Usage

typescript
// Basic - serve files from ./public at root
app.static("./public");

// With prefix - serve at /assets/*
app.static("./assets", { prefix: "/assets" });

File Structure

project/
├── public/
│   ├── index.html
│   ├── style.css
│   └── app.js
├── src/
│   └── index.ts
typescript
app.static("./public");

// Accessible at:
// GET /          -> public/index.html
// GET /style.css -> public/style.css
// GET /app.js    -> public/app.js

SPA Support

For Single Page Applications:

typescript
app.static("./public", { 
  prefix: "/",
  index: "index.html"  // Fallback to index.html for client-side routing
});

Security

  • Prevents directory traversal (cannot access files outside root directory)
  • Directory listing is automatically disabled
  • Missing files return 404

Released under the MIT License.