Static Plugin â
This plugin can serve static files/folders for Elysia Server
Install with:
bun add @elysiajs/static
bun add @elysiajs/static
Then use it:
import { Elysia } from 'elysia'
import { staticPlugin } from '@elysiajs/static'
new Elysia()
.use(staticPlugin())
.listen(8080)
import { Elysia } from 'elysia'
import { staticPlugin } from '@elysiajs/static'
new Elysia()
.use(staticPlugin())
.listen(8080)
By default, the static plugin default folder is public
, and registered with /public
prefix.
Suppose your project structure is:
| - src
| - index.ts
| - public
| - takodachi.png
| - nested
| - takodachi.png
| - src
| - index.ts
| - public
| - takodachi.png
| - nested
| - takodachi.png
The available path will become:
- /public/takodachi.png
- /public/nested/takodachi.png
Config â
Below is a config which is accepted by the plugin
assets â
@default "public"
Path to the folder to expose as static
prefix â
@default "/public"
Path prefix to register public files
ignorePatterns â
@default []
List of files to ignore from serving as static files
staticLimits â
@default 1024
By default, the static plugin will register paths to the Router with a static name, if the limits are exceeded, paths will be lazily added to the Router to reduce memory usage. Tradeoff memory with performance.
alwaysStatic â
@default false
If set to true, static files will path will be registered to Router skipping the staticLimits
.
headers â
@default {}
Set response headers of files
Pattern â
Below you can find the common patterns to use the plugin.
Single file â
Suppose you want to return just a single file, you can use Bun.file
instead of using the static plugin
new Elysia()
.get('/file', () => Bun.file('public/takodachi.png'))
new Elysia()
.get('/file', () => Bun.file('public/takodachi.png'))