Aller au contenu

Cette page n'est pas encore traduite. Elle s'affiche en anglais.

Serve static files

Serve a directory with elide serve: how requests map to files, content types, headers, caching and binding for production.

Lire hors ligneelide help --lang fr static-serving

Sur cette page

elide serve DIR serves a directory over HTTP/1.1. It sends files as they are on disk: no reload, compression, cache headers or TLS. For reload while editing, see Development server; to build the directory, see Static sites.

Terminal
bash
elide serve ./dist
elide serve --host 0.0.0.0 --port 4000 ./dist
FlagDefaultEffect
--host HOST127.0.0.1Bind address; a hostname binds its first address.
--port PORT8080Bind port.
--no-tuiOffPrint the address only; requests are not logged.

Without a terminal, as in CI or a pipe, elide serve behaves as with --no-tui. In Tern, elide serve opens the Server page, with requests, traces and logs.

With no directory argument, elide serve serves the first of:

  1. .dev/sites/main
  2. The only directory in .dev/sites/
  3. The current directory

A file argument runs as a script instead; see Run a fetch handler.

Requests and files

RequestResponse
/index.html
/docs or /docs/docs/index.html, without a redirect
A directory without index.html404; no directory listings
/about when only about.html exists404
/app.js?v=3app.js; the query is ignored
/my%20page.htmlmy page.html; paths are percent-decoded
.., %2e%2e or \ in the path404
A dot file or directory, such as /.env404; .well-known is served
A symbolic linkServed if its target is inside the directory, else 404
A FIFO or other non-regular file404

/docs serves docs/index.html at the URL /docs, so relative links in that page resolve against /. Link to /docs/ or use root-relative URLs.

MethodResponse
GET200 with the file
HEAD200 with the same headers and no body
Others405 with Allow: GET, HEAD

Headers and caching

HeaderValue
Content-TypeFrom the file extension; see below
Content-LengthFile size
DateResponse time
Serverelide
  • No ETag, Last-Modified, Cache-Control or Accept-Ranges. If-None-Match, If-Modified-Since and Range requests get a full 200.
  • Responses are not compressed, and precompressed siblings such as app.js.gz are only served when requested by name.
ExtensionContent-Type
html, htmtext/html; charset=utf-8
csstext/css
js, mjstext/javascript
jsonapplication/json
xmlapplication/xml
txttext/plain
wasmapplication/wasm
png, gif, webpimage/png, image/gif, image/webp
jpg, jpegimage/jpeg
svgimage/svg+xml
icoimage/x-icon
woff, woff2font/woff, font/woff2
pdf, zipapplication/pdf, application/zip
Anything elseapplication/octet-stream

Extensions are case-sensitive: logo.PNG is sent as application/octet-stream. So are .mp4, .avif, .map and .webmanifest.

File changes

elide serve runs one worker thread per CPU core. Each keeps files up to 4 MiB in memory and checks their size and modification time at most once a second, so edits and deletions show up within about a second. Larger files are read from disk on every request.

In production

  • The default host accepts only local connections. Pass --host 0.0.0.0, or an interface address, to accept others.
  • For TLS, HTTP/2, compression or cache headers, put a reverse proxy or CDN in front.
  • For routing, APIs or custom headers, write a handler instead; see Build an HTTP service.

Cette page vous a-t-elle aidé ?