Like Express, and Fastify.

You define route using method as a building block for your server.

By calling .[method name](path, callback, hook?), you attach the route to Elysia, and the library will handle the routing for you.

For example:

new Elysia().get("/ping", () => "pong").listen(8080);

Callback run when the request match.

Means when the [GET] request with path '/ping' shall return 'pong' as a response.

Custom Method

You can define with many built-in methods including:

  • post
  • put
  • patch
  • delete
  • options
  • head
  • trace
  • connect
  • all (map any method)

Other methods can be defined by using .route:

new Elysia()
  // custom method should be all uppercased
  .route("M-SEARCH", () => "From M Search")

Path Parameters

Path parameters can retrieve data from URL.

For example, getting a user id from path (like many social media) is when you may need path parameters.

new Elysia().get("/id/:id", ({ params: { id } }) => getUserById(id)).listen(8080);

Path parameters are a variable in a path.

When the pattern matches, the path you named will become available in context.params, and you can retrieve its value.


Sometimes path parameters are not enough.

Matching anything after some pattern is required, and you can't define them all or its too redundant.

You can use * to match the rest.

new Elysia().get("/user/*", () => doSomething()).listen(8080);