Skip to content

Path

A path or pathname is an identifier to locate resources of a server.

bash
http://localhost:/path/page

Elysia uses the path and method to look up the correct resource.

URL Representation

A path starts after the origin. Prefix with / and ends before search query (?)

We can categorize the URL and path as follows:

URLPath
http://site.com//
http://site.com/hello/hello
http://site.com/hello/world/hello/world
http://site.com/hello?name=salt/hello
http://site.com/hello#title/hello

TIP

If the path is not specified, the browser and web server will treat the path as '/' as a default value.

Elysia will look up each request for route and response using handler function.

Dynamic path

URLs can be both static and dynamic.

Static paths are hardcoded strings that can be used to locate resources of the server, while dynamic paths match some part and captures the value to extract extra information.

For instance, we can extract the user ID from the pathname. For example:

typescript
import { 
Elysia
} from 'elysia'
new
Elysia
()
.
get
('/id/:id', ({
params
: {
id
} }) =>
id
)
.
listen
(3000)

Here dynamic path is created with /id/:id which tells Elysia to match any path up until /id. What comes after that is then stored as params object.

localhost

GET

1

When requested, the server should return the response as follows:

PathResponse
/id/11
/id/123123
/id/anythinganything
/id/anything?name=saltanything
/idNot Found
/id/anything/restNot Found

Dynamic paths are great to include things like IDs, which then can be used later.

We refer to the named variable path as path parameter or params for short.

Segment

URL segments are each path that is composed into a full path.

Segments are separated by /. Representation of URL segments

Path parameters in Elysia are represented by prefixing a segment with ':' followed by a name. Representation of path parameter

Path parameters allow Elysia to capture a specific segment of a URL.

The named path parameter will then be stored in Context.params.

RoutePathParams
/id/:id/id/1id=1
/id/:id/id/hiid=hi
/id/:name/id/hiname=hi

Multiple path parameters

You can have as many path parameters as you like, which will then be stored into a params object.

typescript
import { 
Elysia
} from 'elysia'
new
Elysia
()
.
get
('/id/:id', ({
params
: {
id
} }) =>
id
)
.
get
('/id/:id/:name', ({
params
: {
id
,
name
} }) =>
id
+ ' ' +
name
)
.
listen
(3000)
localhost

GET

1

The server will respond as follows:

PathResponse
/id/11
/id/123123
/id/anythinganything
/id/anything?name=saltanything
/idNot Found
/id/anything/restanything rest

Wildcards

Dynamic paths allow capturing certain segments of the URL.

However, when you need a value of the path to be more dynamic and want to capture the rest of the URL segment, a wildcard can be used.

Wildcards can capture the value after segment regardless of amount by using "*".

typescript
import { 
Elysia
} from 'elysia'
new
Elysia
()
.
get
('/id/*', ({
params
}) =>
params
['*'])
.
listen
(3000)
localhost

GET

1

In this case the server will respond as follows:

PathResponse
/id/11
/id/123123
/id/anythinganything
/id/anything?name=saltanything
/idNot Found
/id/anything/restanything/rest

Wildcards are useful for capturing a path until a specific point.

TIP

You can use a wildcard with a path parameter.

Summary

To summarize, the path in Elysia can be grouped into 3 types:

  • static paths - static string to locate the resource
  • dynamic paths - segment can be any value
  • wildcards - path until a specific point can be anything

You can use all of the path types together to compose a behavior for your web server.

The priorities are as follows:

  1. static paths
  2. dynamic paths
  3. wildcards

If the path is resolved as the static wild dynamic path is presented, Elysia will resolve the static path rather than the dynamic path

typescript
import { 
Elysia
} from 'elysia'
new
Elysia
()
.
get
('/id/1', () => 'static path')
.
get
('/id/:id', () => 'dynamic path')
.
get
('/id/*', () => 'wildcard path')
.
listen
(3000)
localhost

GET

Here the server will respond as follows:

PathResponse
/id/1static path
/id/2dynamic path
/id/2/awildcard path