Overview
The file serving feature turns any directory into a web server instantly. It combines a static file server with automatic tunneling to create a public HTTPS URL, making it perfect for:- Sharing HTML demos and prototypes
- Testing static sites before deployment
- Serving files to mobile devices
- Quick file sharing over the internet
How It Works
When you runahh serve, the CLI:
- Starts a local HTTP server in your current directory
- Creates a Cloudflare tunnel to expose it publicly
- Generates a public HTTPS URL with QR code
- Serves files with proper MIME types
- Automatically serves
index.htmlfor directory requests
The server runs on port 8000 by default but can be customized with the
--port flag.Basic Usage
Serve Current Directory
Serve on Custom Port
Example Output
Implementation Details
Server Creation
The server is built using Elysia with CORS support:/home/daytona/workspace/source/ahh-binary/src/commands/serve/main.ts:9-53
Path Security
The implementation includes security measures to prevent directory traversal:..sequences are removed from paths- Files can only be served from the current directory and subdirectories
- Attempts to access parent directories return 403 Forbidden
/home/daytona/workspace/source/ahh-binary/src/commands/serve/main.ts:15-27
Automatic Index Files
When a directory is requested, the server automatically looks forindex.html:
/home/daytona/workspace/source/ahh-binary/src/commands/serve/main.ts:18-23
MIME Type Detection
Files are served with correct Content-Type headers using themime library:
/home/daytona/workspace/source/ahh-binary/src/commands/serve/main.ts:30-36
Error Handling
The server handles common errors gracefully:/home/daytona/workspace/source/ahh-binary/src/commands/serve/main.ts:39-44
Use Cases
Demo Sharing
Share HTML/CSS/JS demos with clients or teammates without deployment.
Mobile Testing
Scan the QR code to test your static site on mobile devices instantly.
Static Site Preview
Preview static site generators (Hugo, Jekyll, etc.) before deploying.
File Distribution
Quickly share files with others over the internet.
Directory Structure
Here’s how different file structures are served:- Simple Site
- Multi-Page Site
- Build Output
https://your-url/→ servesindex.htmlhttps://your-url/style.css→ servesstyle.csshttps://your-url/script.js→ servesscript.js
Parallel Execution
The serve command starts the server and tunnel in parallel for faster startup:/home/daytona/workspace/source/ahh-binary/main.ts:69-72
Security Features
Path Traversal Protection
✅ Protected:CORS Configuration
The server enables CORS for all origins:- Cross-origin requests from any domain
- Testing with different frontend origins
- Mobile device access
Supported File Types
The server automatically detects and serves files with correct MIME types:| Extension | MIME Type | Example |
|---|---|---|
.html | text/html | index.html |
.css | text/css | style.css |
.js | application/javascript | app.js |
.json | application/json | data.json |
.png | image/png | logo.png |
.jpg | image/jpeg | photo.jpg |
.svg | image/svg+xml | icon.svg |
.pdf | application/pdf | document.pdf |
.woff2 | font/woff2 | font.woff2 |
application/octet-stream.
Advanced Usage
Serve Specific Build Directories
Custom Port for Multiple Servers
Run multiple servers on different ports:Programmatic Usage
Troubleshooting
404 Not Found for files that exist
404 Not Found for files that exist
Check:
- File path case sensitivity (Linux/Mac are case-sensitive)
- File is in current directory or subdirectory
- You’re running
ahh servefrom the correct directory - File name matches URL exactly
403 Forbidden errors
403 Forbidden errors
This means you’re trying to access files outside the current directory:
- Don’t use
..in URLs - Ensure you’re in the correct directory when starting the server
- Check that the file exists in a subdirectory of where you ran the command
Wrong MIME type / file not rendering
Wrong MIME type / file not rendering
- Ensure file extension is correct (e.g.,
.cssnot.txt) - Check browser console for MIME type errors
- Verify file contents are valid for that type
Port already in use
Port already in use
Best Practices
- Organize your files - Use a clean directory structure with an
index.htmlat the root - Build before serving - Run your build process before serving production builds
- Check the directory - Always verify you’re in the correct directory before running
ahh serve - Use relative paths - In your HTML, use relative paths for assets (
./style.cssnot/style.css) - Test mobile early - Use the QR code to test on mobile devices during development
Related Commands
- Tunneling - Learn more about tunnel creation and management
- Webhook Server - Receive HTTP requests instead of serving files
Technical Reference
Command Options
| Option | Alias | Type | Default | Description |
|---|---|---|---|---|
--port | -p | number | 8000 | Port to run the server on |
Return Value
ThecreateSimpleServer function returns:
Dependencies
- Elysia: Fast web framework
- @elysiajs/cors: CORS middleware
- mime: MIME type detection
- Cloudflare tunnel: For public URL generation
