| title | Your first route |
|---|---|
| description | Densky has a file-based routing which means that you just need to create a file to get your first route. |
All the routes must be in the src/routes, to make your own route create a file on there.
+ routes
+ | _index.tsexport function GET() {
return new Response("Hello World");
}Once you have written your route, use the densky-cli dev command:
densky devTo set a dynamic route you need a $ at start of the route filename. For example:
routes
+ | user
+ | | $id.ts
| _index.tsimport { type HTTPRequest } from "densky";
export function GET(req: HTTPRequest) {
const userId = req.params.get("id");
return new Request(`Viewing user ${userId}`);
}Make a new HTTP method entry is very easy, for this example we will create a the _index.ts file in routes/user and it will create a new user using the POST method.
routes
| user
+ | | _index.ts
| | $id.ts
| _index.tsimport { type HTTPRequest } from "densky";
export async function POST(req: HTTPRequest) {
const userData = await req.raw.json();
return new Request(`Viewing user ${userId}`);
}In the code above you can see a couple of new things.
You can export methods on the routes as async function, in this case it is required to use the method at the below tick.
Densky use a request wrapper to extend the functionalities, but for now the request data isn't handled and it should be used with the raw request.
Okey, What is a raw request? The request wrapper contains a property named raw which its type is Request. See more on API Documentation.