Overview
Theserve command starts a local HTTP server that serves files from the current directory and automatically creates a Cloudflare tunnel to make it accessible over the internet. It’s perfect for quickly sharing static files, websites, or assets.
Syntax
Parameters
The port number to run the HTTP server on.Aliases:
-pUsage Examples
Output
The command displays:- A spinner: “Starting server, this may take a second…”
- The public tunnel URL
- A QR code for the URL
Example Output
How It Works
Server Behavior
The HTTP server:- Serves files from the current working directory
- Enables CORS for cross-origin requests
- Automatically serves
index.htmlfor directory requests - Returns appropriate MIME types based on file extensions
- Handles path traversal attacks by validating paths
Directory Index
When accessing a directory (URL ending with/):
- Checks for
index.htmlin that directory - Serves
index.htmlif it exists - Returns 404 if no index file is found
Security
The server:- Removes
..segments from paths - Validates all paths start with the current directory
- Returns 403 Forbidden for invalid paths
Error Handling
File Not Found (404)
Returned when a requested file doesn’t exist:Forbidden (403)
Returned when attempting to access paths outside the served directory:Internal Server Error (500)
Returned for unexpected server errors:Technical Details
Parallel Execution
The command runs two operations in parallel:- Starting the local HTTP server on the specified port
- Creating a Cloudflare tunnel to that port
MIME Type Detection
The server automatically detects and sets correct MIME types:.html→text/html.css→text/css.js→application/javascript.json→application/json.png→image/png- Unknown types →
application/octet-stream
Implementation
Built using:- Elysia - Fast HTTP server framework
- @elysiajs/cors - CORS middleware
- mime/lite - MIME type detection
Return Value
The underlyingcreateSimpleServer() function returns:
Notes
The server continues running until you terminate the process with Ctrl+C.
All files in the current directory and subdirectories become accessible via the public URL. Be careful not to expose sensitive files.
For rate limiting information about Cloudflare tunnels, see the tunnel command.
