Routing
Lack uses your file and folder names for grouping your paths. To create actual paths you export functions from your JavaScript (or TypeScript) file.
File system
To define a group you can just create a file.
app/hello.js
- /hello/
To create a path, export a function. The name will be used as the path.
export async function world() {
// ...
}
Routes: GET /hello/world
A folder will also create a group.
export async function world() {
// ...
}
Routes: GET /folder/hello/world
Special paths
To create a path on the index route export a function named $index
export async function $index() {
// ...
}
Routes: GET /hello
Sometimes you dont want to split everything into a file. You can define deeper paths by using two underscores __
export async function a__b() {
// ...
}
export async function a_b() {
// ...
}
Routes:
GET /hello/a/b (1st function)
GET /hello/a_b (2nd function)
This can create path collisions, but Lack will notify you of them if they happen.
Path params
Path params are defined by using $<name>
, so for a path like /post/:id, you would define it by exporting post$id
.
export async function post$id() {
// ...
}
Routes: GET /hello/post/:id
Path params are passed as a normal lambda handler, meaning the 1st argument is an "event" with an pathParameters
object.
AWS Docs for the handler (opens in a new tab)
Methods
Methods are defined by prefixing the function name by one of four names.
Prefix | Method |
---|---|
any$ | ANY |
pos$ | POST |
put$ | PUT |
del$ | DELETE |
If a function doesn't have a prefix, it will be set to the GET method.
Example post endpoint
export async function pos$create() {
// ...
}
Routes: POST /hello/create
Defining the same path with a different method is supported, even in between files.