Route

Web servers use the request's path and HTTP method to look up the correct resource, refers as "routing".

We can define a route by calling a method named after HTTP verbs, passing a path and a function to execute when matched.

import { Application } from "@teakit/core";
new Application()
.get("/", () => "Landing")
.get("/hello", () => "Hi")
.listen(3000);

We can access the web server by going to http://localhost:3000

This code create a web server running at port 3000, and register the following path with the GET method which response as follows:

HTTP Verb

There are many HTTP methods to use in a different situation, for instance.

GET

Requests using GET should only retrieve data.

POST

Submits a payload to the specified resource, often causing state change or side effect.

PUT

Replaces all current representations of the target resource using the request's payload.

DELETE

Deletes the specified resource.

To handle each of the different verbs, @teakit/core has a built-in API for several HTTP verbs by default, similar to Application.get

import { Application } from "@teakit/core";
new Application()
.get("/", () => "hello")
.post("/hi", () => "hi")
.listen(3000);

Application HTTP methods accepts the following parameters:

  • path: Pathname
  • function: Function to respond to the client
  • hook: Additional metadata

You can read more about the HTTP methods on HTTP Request Methods.

Handle

Most developers use REST clients like Postman, Insomnia or Hoppscotch to test their API.

However, Teakit can be programmatically test using Application.handle.

import { Application } from "@teakit/core";
const app = new Application()
.get("/", () => "hello")
.post("/hi", () => "hi")
.listen(3000);
app.handle(new Request("http://localhost/")).then(console.log);

Application.handle is a function to process an actual request sent to the server.

Custom Method

We can accept custom HTTP Methods with Application.route.

import { Application } from "@teakit/core";
const app = new Application()
.get("/", () => "hello")
.post("/", () => "hi")
.route("M-SEARCH", "/", () => "connect") // [!code ++]
.listen(3000);

Application.route accepts the following:

  • method: HTTP Verb
  • path: Pathname
  • function: Function to response to the client
  • hook: Additional metadata

When navigating to each method, you should see the results as the following:

Path Method Result
/
GEThello
/
POSThi
/
M-SEARCHconnect

Based on RFC 7231, HTTP Verb is case-sensitive.

It's recommended to use the UPPERCASE convention for defining a custom HTTP Verb with Application.

Application.all

Application provides an Application.all for handling any HTTP method for a specified path using the same API like Application.get and Application.post

import { Application } from "@teakit/core";
new Application().all("/", () => "hi").listen(3000);

Any HTTP method that matches the path, will be handled as follows:

Path Method Result
/
GEThi
/
POSThi
/
M-SEARCHhi

404

If no path matches the defined routes, Teakit will pass the request to error life cycle before returning a "NOT_FOUND" with an HTTP status of 404.

We can handle a custom 404 error by returning a value from 'error` life cycle like this:

import { Application } from "@teakit/core";
new Application()
.get("/", () => "hi")
.onError(({ code }) => {
if (code === "NOT_FOUND") return "Route not found :(";
})
.listen(3000);

When navigating to your web server, you should see the result as follows:

Path Method Result
/
GEThi
/
POSTRoute not found :(
/hi
GETRoute not found :(
PreviousAt Glance
NextPath