Elysia

Elysia logo

Overview

Elysia is a JavaScript HTTP server library that targets the Bun JavaScript runtime.

Elysia is a competitor to Hono. It has slightly better performance than Hono, but only runs in Bun. Both Elysia and Hono are significantly faster that Express.

These notes are currently quite minimal.

See the example Elysia project at elysia-demos.

Context

The Context object that is passed to routes has many properties.

The most useful properties on the Context object related to extracting data from a request are described in the following table. This assumes that the variable c holds a Context object that was passed to a route function. Typically this is destructured in the argument list.

ActionCode
get value of request headerc.headers['some-name']
get value of path parameterc.params['some-name']
get value of query parameterc.query['some-name']
get value of text bodyconst text = c.body;
get form data from bodyconst formData = c.body;
get property from formDataconst value = formData.some-name;
get value of JSON bodyconst object = c.body;

Elysia determines how to parse request bodies based on the value of the Content-Type request header. See Explicit Body.

The most useful properties on the Context object related to creating a response are described in the following table.

ActionCode
set value of a response headerc.header('Some-Name', 'some value');
set status codec.status(someCode);
return text responsereturn 'some text';
return JSON responsereturn someObject;
return HTML responsereturn someJSX;
return "Not Found" errorset.status = 404; return 'Not Found';
redirect to another URLset.redirect = 'someURL'

Returning JSX that is converted to HTML requires the following setup:

  1. bun add @elysiajs/html
  2. Import with import {html} from '@elysiajs/html';
  3. Configure with app.use(html());