Skip to content
On this page

Routing ​

Netzo provides a routing module that allows you to specify how requests are handled by your application.

Routing Syntax ​

The routing syntax allows for specifying:

  • Simple routes such as /home
  • Path parameters such as "blog/:slug" which are made available as the third argument of the handler function
  • Capturing all routes using the wildcard "*"

Additionally, one can specify a custom 404 handler for requests that don't match any routes, this will override the default "Not found" handler.

Order of Routes

It is important to note that when utilizing the serve function, routes are processed in a sequential manner and only one function will be executed per request.

Route Handlers ​

Route handlers are functions that are called when a request is made to a particular route. These functions are responsible for processing the request and generating a response. They are defined in the form of Request => Response or Request => Promise<Response>.

When using the serve function, at least one entrypoint handler is required to handle incoming requests. The serve function expects a single handler argument, which can be an object containing multiple route handlers, or a single handler function.

Handlers receive a Request object as the first argument, and have the option to receive additional arguments such as ConnInfo and Params. The handler must return a Response object, or a Promise resolving to a Response object (if async).

ts
serve({
  '/:slug': (request: Request, connInfo: ConnInfo, params: Params) => {
    return new Response(`At page with slug ${params.slug}`)
  }
})
serve({
  '/:slug': (request: Request, connInfo: ConnInfo, params: Params) => {
    return new Response(`At page with slug ${params.slug}`)
  }
})

Examples ​

Here is an example of how to use the serve function:

ts
import { serve, jsx, json, serveStatic } from 'https://deno.land/x/[email protected]/mod.ts'

serve({
  '/': () => new Response ('Hello World!'),
  '/ui': ()  => jsx(<App />),
  '/api': () => json({ message: 'Hello World!' }, { status: 200 }),
  '/public': serveStatic('public/index.html', { baseURL: import.meta.url }),
})
import { serve, jsx, json, serveStatic } from 'https://deno.land/x/[email protected]/mod.ts'

serve({
  '/': () => new Response ('Hello World!'),
  '/ui': ()  => jsx(<App />),
  '/api': () => json({ message: 'Hello World!' }, { status: 200 }),
  '/public': serveStatic('public/index.html', { baseURL: import.meta.url }),
})

The serve function takes an object with key-value pairs where the key is the URL path and the value is a callback function that will be executed when a request is made to that path.

The code above shows how to handle 3 different routes:

  • The root URL "/": when a client makes a request to this route, the server will respond with "Hello World!" as plain text.
  • The UI endpoint "/ui": when a client makes a request to this route, the server will serve a JSX App.
  • The API endpoint "/api": when a client makes a request to this route, the server will respond with a JSON object { message: "Hello World!"} and a status code of 200.
  • The "/public" endpoint: when a client makes a request to this route, the server will serve the public/index.html file as the response.

In addition, the code also imports the json and serveStatic functions from the https://deno.land/x/[email protected]/mod.ts module that are used to send json responses and serve static files respectively.