std@0.116.0/http

Deno standard library
GitHub Stars
2187
Go to latest
import * as mod from "https://deno.land/std@0.116.0/http/mod.ts";

Classes

Server

Used to construct an HTTP server.

Enums

Status

Enum of HTTP status codes.

Variables

STATUS_TEXT

Map from status code to status text.

Functions

_parseAddrFromStr

Parse an address from a string.

deleteCookie

Set the cookie header with empty value in the headers to delete it

getCookies

Parse cookies of a header

listenAndServe 👎 deprecated
listenAndServeTls 👎 deprecated
serve

Serves HTTP requests with the given handler.

serveListener

Constructs a server, accepts incoming connections on the given listener, and handles requests on these connections with the given handler.

serveTls

Serves HTTPS requests with the given handler.

setCookie

Set the cookie header properly in the headers

Interfaces

ConnInfo

Information about the connection a request arrived on.

Cookie
ServeInit

Additional serve options.

ServerInit

Options for running an HTTP server.

Type Aliases

Handler

A handler for HTTP requests. Consumes a request and connection information and returns a response.

http

http is a module to provide HTTP client and server implementations.

Server

Server APIs utilizing Deno's HTTP server APIs.

import { serve } from "https://deno.land/std@0.116.0/http/server.ts";

serve(() => new Response("Hello World\n"));

console.log("http://localhost:8000/");

Legacy Server (Deprecated)

Legacy server APIs using a JavaScript HTTP server implementation.

import { serve } from "https://deno.land/std@0.116.0/http/server_legacy.ts";

const server = serve({ port: 8000 });
console.log("http://localhost:8000/");

for await (const req of server) {
  req.respond({ body: "Hello World\n" });
}

File Server

A small program for serving local files over HTTP.

deno run --allow-net --allow-read https://deno.land/std/http/file_server.ts
> HTTP server listening on http://localhost:4507/

HTTP Status Code and Status Text

Helper for processing status code and status text.

import {
  Status,
  STATUS_TEXT,
} from "https://deno.land/std@0.116.0/http/http_status.ts";

console.log(Status.NotFound); //=> 404
console.log(STATUS_TEXT.get(Status.NotFound)); //=> "Not Found"

Helpers to manipulate the Cookie header.

getCookies

import { getCookies } from "https://deno.land/std@0.116.0/http/cookie.ts";

const headers = new Headers();
headers.set("Cookie", "full=of; tasty=chocolate");

const cookies = getCookies(headers);
console.log(cookies); // { full: "of", tasty: "chocolate" }

setCookie

import {
  Cookie,
  setCookie,
} from "https://deno.land/std@0.116.0/http/cookie.ts";

const headers = new Headers();
const cookie: Cookie = { name: "Space", value: "Cat" };
setCookie(headers, cookie);

const cookieHeader = headers.get("set-cookie");
console.log(cookieHeader); // Space=Cat

deleteCookie

Note: Deleting a Cookie will set its expiration date before now. Forcing the browser to delete it.

import { deleteCookie } from "https://deno.land/std@0.116.0/http/cookie.ts";

const headers = new Headers();
deleteCookie(headers, "deno");

const cookieHeader = headers.get("set-cookie");
console.log(cookieHeader); // deno=; Expires=Thus, 01 Jan 1970 00:00:00 GMT

Note: It is possible to pass the exact same path and domain attributes that were used to set the cookie.

import { deleteCookie } from "https://deno.land/std@0.116.0/http/cookie.ts";

const headers = new Headers();
deleteCookie(headers, "deno", { path: "/", domain: "deno.land" });

Note: At the moment multiple Set-Cookie in a Response is not handled.