Static plugin
This plugin can serve static files/folders for an Elysia server
Install with:
bun add @elysia/static@nextThen use it:
import { Elysia } from 'elysia'import { staticPlugin } from '@elysia/static'
new Elysia() .use(staticPlugin()) .listen(3000)By default, the static plugin’s folder is public, and it is registered with the /public prefix.
Suppose your project structure is:
| - src | - index.ts| - public | - takodachi.png | - nested | - takodachi.pngThe available paths will become:
- /public/takodachi.png
- /public/nested/takodachi.png
Config
Section titled “Config”Below is a config which is accepted by the plugin
assets
Section titled “assets”Default: "public"
Path to the folder to expose as static
prefix
Section titled “prefix”Default: "/public"
Path prefix to register public files
ignorePatterns
Section titled “ignorePatterns”Default: ['.DS_Store', '.git', '.env']
List of files to ignore from serving as static files
staticLimit
Section titled “staticLimit”Default: 1024
When alwaysStatic is enabled, the static plugin registers up to this many paths to the Router with a static name. Files past the limit are resolved lazily on request to reduce memory usage. Trade off memory against performance.
alwaysStatic
Section titled “alwaysStatic”Default: process.env.NODE_ENV === 'production'
If set to true, static file paths are registered to the Router at startup, up to staticLimit. Otherwise, files are resolved lazily on request.
headers
Section titled “headers”Default: {}
Set response headers of files
indexHTML
Section titled “indexHTML”Default: true
If enabled, directory requests can serve the index.html file inside that directory. This is not a catch-all single-page application fallback for arbitrary missing paths.
Pattern
Section titled “Pattern”Below you can find the common patterns to use the plugin.
Single file
Section titled “Single file”Suppose you want to return just a single file. You can use file instead of using the static plugin
import { Elysia, file } from 'elysia'
new Elysia() .get('/file', file('public/takodachi.png'))