Skip to content
On this page

http ​

https://deno.land/x/netzo/lib/http

The http module exports routing utilities from the open-source sift module for creating simple HTTP servers based on std/http. You could also use the sift module directly if you want to use the routing utilities in a custom HTTP server.

This module is entirely based on the open-source satyarohith/sift and its documentation.

serve ​

serve() is the routing function. It accepts an object literal with path strings as keys and their corresponding route handlers as values. The path string is processed using URLPattern and when the requested path matches the provided pattern, the corresponding handler is invoked.

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

serve({
  '/': () => new Response('hello world'),
  '/blog/:slug': (request, connInfo, params) => {
    const post = `Hello, you visited ${params.slug}!`
    return new Response(post)
  },
  // The route handler of 404 will be invoked when a route handler
  // for the requested path is not found.
  '404': () => new Response('not found'),
})
import { serve } from 'https://deno.land/x/[email protected]/mod.ts'

serve({
  '/': () => new Response('hello world'),
  '/blog/:slug': (request, connInfo, params) => {
    const post = `Hello, you visited ${params.slug}!`
    return new Response(post)
  },
  // The route handler of 404 will be invoked when a route handler
  // for the requested path is not found.
  '404': () => new Response('not found'),
})

json ​

Converts an object literal to a JSON string and creates a Response instance with application/json as the content-type.

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

serve({
  '/': () => json({ message: 'hello world' }),
  'api/create': () => json({ message: 'created' }, { status: 201 }),
})
import { json, serve } from 'https://deno.land/x/[email protected]/mod.ts'

serve({
  '/': () => json({ message: 'hello world' }),
  'api/create': () => json({ message: 'created' }, { status: 201 }),
})

jsx ​

Renders JSX components to HTML string and creates a Response instance with text/html as the content-type.

Requirements to use JSX/TSX

Make sure the file extension is .jsx or .tsx, to add /** @jsx h */ to the first line and to import the h function for Deno to be able to transform the JSX/TSX code.

tsx
/** @jsx h */
import { h, jsx, serve } from 'https://deno.land/x/[email protected]/mod.ts'

const App = () => (
  <div>
    <h1>Hello world!</h1>
  </div>
)

const NotFound = () => (
  <div>
    <h1>Page not found</h1>
  </div>
)

serve({
  '/': () => jsx(<App />),
  '404': () => jsx(<NotFound />, { status: 404 }),
})
/** @jsx h */
import { h, jsx, serve } from 'https://deno.land/x/[email protected]/mod.ts'

const App = () => (
  <div>
    <h1>Hello world!</h1>
  </div>
)

const NotFound = () => (
  <div>
    <h1>Page not found</h1>
  </div>
)

serve({
  '/': () => jsx(<App />),
  '404': () => jsx(<NotFound />, { status: 404 }),
})