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

x/frugal/docs/dep/std/http.ts>serve

A frugal web framework
Go to Latest
function serve
Re-export
import { serve } from "https://deno.land/x/frugal@0.5.0/docs/dep/std/http.ts";

Serves HTTP requests with the given handler.

You can specify an object with a port and hostname option, which is the address to listen on. The default is port 8000 on hostname "0.0.0.0".

The below example serves with the port 8000.

import { serve } from "https://deno.land/std@0.224.0/http/server.ts";
serve((_req) => new Response("Hello, world"));

You can change the listening address by the hostname and port options. The below example serves with the port 3000.

import { serve } from "https://deno.land/std@0.224.0/http/server.ts";
serve((_req) => new Response("Hello, world"), { port: 3000 });

serve function prints the message Listening on http://<hostname>:<port>/ on start-up by default. If you like to change this message, you can specify onListen option to override it.

import { serve } from "https://deno.land/std@0.224.0/http/server.ts";
serve((_req) => new Response("Hello, world"), {
  onListen({ port, hostname }) {
    console.log(`Server started at http://${hostname}:${port}`);
    // ... more info specific to your server ..
  },
});

You can also specify undefined or null to stop the logging behavior.

import { serve } from "https://deno.land/std@0.224.0/http/server.ts";
serve((_req) => new Response("Hello, world"), { onListen: undefined });

Parameters

handler: Handler

The handler for individual HTTP requests.

optional
options: ServeInit = [UNSUPPORTED]

The options. See ServeInit documentation for details.