Deserve is Still in its early stages, expect breaking changes

Creating an app

This is the core function of this library. createApp function as the name suggests creates a DeserveApp instance

import { createApp } from "https://deno.land/x/deserve/mod.ts"

const app = createApp()

Listen

The listen method allows you to start the app server with the given hostname and port

/// ...
app.listen({ port: 8080 })

Context

The createApp method optionally takes in function that takes the current request and returns an object called the context that can be accessed by all other Handlers registered. Click here to learn more about context

Handlers

Handlers are the building blocks of your server, these are similar to (but NOT the same) as middleware, if you are familiar with middleware based frameworks

Handler is basically a function that takes in Request as an argument and can return a Response to fulfill the request or return nothing to continue to the next registered handler

Handlers also take an optional argument called the context which you can learn more about here

use the use method on the app object to register handlers

app.use(...handlers)

Example

app.use((request) => {
    return new Response("Hello, World!")
})

Example with Multiple Handlers

app.use((request) => {
    console.log(`new request at ${request.url}`);
}, () => {
    return new Response("Hello, World!")
})

Here the first handler logs the url of every incoming request and because it doesn't return a response the next handler will be called and in the above example the second handler returns a response so the execution ends and Hello World is returned

NOTE: The use method can be called multiple times to achieve the same effect

Complete Example

import { createApp } from "https://deno.land/x/deserve/mod.ts"

const app = createApp()

app.use((request) => {
    console.log(`new request at ${request.url}`);
}, () => {
    return new Response("Hello, World!")
})

app.listen({
    port: 8080
})

Path restricted handlers

These are the same as normal handlers but only run when the current path matches the given path pattern

The path patterns follow the web standard URLPattern API

Example

/// ...

app.use("/", () => response("Home Page"))
app.use("/contact", () => response("Contact Page"))

app.use("/blog/:slug", (request, context) => {
    const { slug } = context.params;

    return response(`Blog with slug ${slug}`)
})

/// ...