Skip to main content
Deno 2 is finally here 🎉️
Learn more
Module

x/fastro/http/server.ts>Fastro

Fast and simple web application framework for deno
Go to Latest
interface Fastro
import { type Fastro } from "https://deno.land/x/fastro@v0.77.3/http/server.ts";

Properties

close: () => void

Immediately close the server listeners and associated HTTP connections.

Methods

use(...handler: Array<HandlerArgument>): Fastro

Add application level middleware

Example

import fastro from "../mod.ts";

const f = new fastro();
f.use((req: HttpRequest, _ctx: Context, next: Next) => {
  console.log(`${req.method} ${req.url}`);
  return next();
});

await f.serve();
get(path: string, ...handler: Array<HandlerArgument>): Fastro
post(path: string, ...handler: Array<HandlerArgument>): Fastro
put(path: string, ...handler: Array<HandlerArgument>): Fastro
delete(path: string, ...handler: Array<HandlerArgument>): Fastro
patch(path: string, ...handler: Array<HandlerArgument>): Fastro
options(path: string, ...handler: Array<HandlerArgument>): Fastro
head(path: string, ...handler: Array<HandlerArgument>): Fastro
hook(hook: Hook): Fastro

Allow you access Server, Request, and Info after Middleware, Routes, Pages, and Static File Processing. It can return Response, Promise<Response> or void.

Example

import fastro, { Fastro, Info } from "../mod.ts";

const f = new fastro();

f.hook((_f: Fastro, _r: Request, _i: Info) => new Response("Hello World"));

await f.serve();
static(path: string, options?: { maxAge?: number; folder?: string; }): Fastro

Allow you to access static files with custom path, folder, and maxAge

Example

import fastro from "../mod.ts";

const f = new fastro();

f.static("/static", { folder: "static", maxAge: 90 });

await f.serve();
page(
path: string,
element: Component,
...handler: Array<MiddlewareArgument>,
): Fastro

Allow you to define SSR page with custom path, element, and handler

Example

import fastro, { Context, HttpRequest } from "../mod.ts";
import user from "../pages/user.tsx";

const f = new fastro();

f.static("/static", { folder: "static", maxAge: 90 });

f.page("/", user, (_req: HttpRequest, ctx: Context) => {
    const options = {
      props: { data: "Guest" },
      status: 200,
      html: { head: { title: "React Component" } },
    };
    return ctx.render(options);
  },
);
await f.serve();
getStaticFolder(): string
getStaticPath(): string
getDevelopmentStatus(): boolean
push(
method?: string,
path?: string,
...handler: Array<HandlerArgument>,
): Fastro

Add a handler directly

onListen(handler: ListenHandler): void
finished(): Promise<void> | undefined
getNest(): Nest
serve(): Promise<void>

Serves HTTP requests

If the server was constructed without a specified port, 8000 is used.