Skip to content

Latest commit

 

History

History
73 lines (56 loc) · 1.97 KB

File metadata and controls

73 lines (56 loc) · 1.97 KB
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.

Simple route

All the routes must be in the src/routes, to make your own route create a file on there.

+ routes
+ | _index.ts
export function GET() {
    return new Response("Hello World");
}

Once you have written your route, use the densky-cli dev command:

densky dev

Making dynamic route

To set a dynamic route you need a $ at start of the route filename. For example:

  routes
+ | user
+ | | $id.ts
  | _index.ts
import { type HTTPRequest } from "densky";

export function GET(req: HTTPRequest) {
    const userId = req.params.get("id");
    return new Request(`Viewing user ${userId}`);
}

Making POST entry

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.ts
import { 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.

1. async keyword

You can export methods on the routes as async function, in this case it is required to use the method at the below tick.

2. req.raw.json() What the hell is that?

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.