2 unstable releases
Uses new Rust 2024
| new 0.2.0 | Feb 25, 2026 |
|---|---|
| 0.1.0 | Feb 25, 2026 |
#282 in HTTP server
15KB
346 lines
serve
Serve a single local file over HTTP. One file in, one URL out.
Install
cargo install serve-it
Usage
# Serve a file on the default port (8000)
serve ./data.json
# → http://127.0.0.1:8000/data.json
# Serve on a custom port
serve ./index.html 9000
# → http://127.0.0.1:9000/index.html
The file is served at a path matching its filename. Any other path returns a 404.
Options
| Argument | Type | Required | Default | Description |
|---|---|---|---|---|
PATH |
PathBuf |
yes | Path to the file to serve | |
PORT |
u16 |
no | 8000 |
Port to listen on |
Response Headers
Every response includes:
Content-Typematching the file extensionContent-LengthAccess-Control-Allow-Origin: *(CORS)Cache-Control: no-cache
Supported MIME Types
| Extension | Content-Type |
|---|---|
.json |
application/json |
.html, .htm |
text/html |
.css |
text/css |
.js |
application/javascript |
.txt |
text/plain |
.xml |
application/xml |
.csv |
text/csv |
.svg |
image/svg+xml |
.png |
image/png |
.jpg, .jpeg |
image/jpeg |
.gif |
image/gif |
.wasm |
application/wasm |
.pdf |
application/pdf |
.toml |
application/toml |
.yaml, .yml |
application/yaml |
| (other) | application/octet-stream |
Development
# Build
cargo build
# Run tests
cargo test
# Lint
cargo clippy -- -D warnings
# Format check
cargo fmt -- --check
Git hooks via lefthook:
lefthook install
License
MIT
Dependencies
~1–1.5MB
~27K SLoC