Static File Serving
Nile can serve static files from a local directory through the REST interface. This is useful for serving images, documents, or any assets your application needs to expose over HTTP.
Configuration
Enable static file serving with enableStatic and specify your server runtime:
How It Works
When enableStatic is true, Nile registers a middleware on /assets/* that serves files from a ./assets directory relative to your project root.
These files are then accessible at:
Cross-Runtime Support
Nile dynamically imports the correct serveStatic adapter based on the runtime value:
The adapter is lazy-loaded on the first request to /assets/*, not at startup. This avoids runtime-specific import issues during testing or mixed environments.
If the adapter fails to load (e.g., the package isn't installed), static serving is silently disabled and requests to /assets/* fall through to the 404 handler.
Node Requirement
When using runtime: "node", make sure the Node adapter is installed:
The Bun adapter ships with Hono by default, so no extra install is needed for Bun.
Middleware Order
Static file serving runs after CORS and rate limiting, but before the main service endpoint:
- CORS
- Rate limiting
- Static file serving (
/assets/*) POST {baseUrl}/services(RPC endpoint)GET /status(health check)- 404 handler
This means static file requests respect your CORS and rate limiting configuration.