Skip to content

Static plugin

This plugin can serve static files/folders for an Elysia server

Install with:

Terminal window
bun add @elysia/static@next

Then 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.png

The available paths will become:

  • /public/takodachi.png
  • /public/nested/takodachi.png

Below is a config which is accepted by the plugin

Default: "public"

Path to the folder to expose as static

Default: "/public"

Path prefix to register public files

Default: ['.DS_Store', '.git', '.env']

List of files to ignore from serving as static files

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.

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.

Default: {}

Set response headers of files

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.

Below you can find the common patterns to use the plugin.

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'))

Type to search the documentation.

    Up arrowDown arrow to navigateEnter to selectEscape to close