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

std/http/mod.ts

Deno standard library
Go to Latest
The Standard Library has been moved to JSR. See the blog post for details.
import * as mod from "https://deno.land/std@0.193.0/http/mod.ts";

Provides ServerSentEvent and ServerSentEventStreamTarget which provides an interface to send server sent events to a browser using the DOM event model.

The ServerSentEventStreamTarget provides the .asResponse() or .asResponseInit() to provide a body and headers to the client to establish the event connection. This is accomplished by keeping a connection open to the client by not closing the body, which allows events to be sent down the connection and processed by the client browser.

See more about Server-sent events on MDN

Example

import {
  ServerSentEvent,
  ServerSentEventStreamTarget,
} from "https://deno.land/std@0.193.0/http/server_sent_event.ts";
import { serve } from "https://deno.land/std@0.193.0/http/server.ts";

await serve((request) => {
  const target = new ServerSentEventStreamTarget();
  let counter = 0;

  // Sends an event every 2 seconds, incrementing the ID
  const id = setInterval(() => {
    const evt = new ServerSentEvent(
      "message",
      { data: { hello: "world" }, id: counter++ },
    );
    target.dispatchEvent(evt);
  }, 2000);

  target.addEventListener("close", () => clearInterval(id));
  return target.asResponse();
}, { port: 8000 });

Classes

Provides a way to manage cookies in a request and response on the server as a single iterable collection.

The base class that all derivative HTTP extend, providing a status and an expose property.

Provides an way to manage cookies in a request and response on the server as a single iterable collection, as well as the ability to sign and verify cookies to prevent tampering.

Used to construct an HTTP server.

An event which contains information which will be sent to the remote connection and be made available in an EventSource as an event. A server creates new events and dispatches them on the target which will then be sent to a client.

An implementation of ServerSentEventTarget that provides a readable stream as a body of a response to establish a connection to a client.

Enums

Standard HTTP status codes.

Variables

Symbol which is used in mergeHeaders to extract a [string | string][] from an instance to generate the final set of headers.

A namespace that contains each error constructor. Each error extends HTTPError and provides .status and .expose properties, where the .status will be an error Status value and .expose indicates if information, like a stack trace, should be shared in the response.

A constant array of common HTTP methods.

A record of all the status codes text.

Functions

Returns an array of media types accepted by the request, in order of preference. If there are no media types supplied in the request, then any media type selector will be returned.

Returns an array of content encodings accepted by the request, in order of preference. If there are no encoding supplied in the request, then ["*"] is returned, implying any encoding is accepted.

Returns an array of languages accepted by the request, in order of preference. If there are no languages supplied in the request, then ["*"] is returned, imply any language is accepted.

Calculate an ETag for an entity. When the entity is a specific set of data it will be fingerprinted as a "strong" tag, otherwise if it is just file information, it will be calculated as a weak tag.

A factory function which provides a way to create errors. It takes up to 3 arguments, the error Status, an message, which defaults to the status text and error options, which includes the expose property to set the .expose value on the error.

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

Parse cookies of a header

Parse set-cookies of a header

A helper function that takes the value from the If-Match header and a calculated etag for the target. By using strong comparison, return true if the values match, otherwise false.

A helper function that takes the value from the If-None-Match header and a calculated etag for the target entity and returns false if the etag for the entity matches the supplied value, otherwise true.

A type guard that determines if the status code is a client error.

A type guard that determines if the status code is an error.

A type guard that determines if the value is an HttpError or not.

A type guard that determines if a value is a valid HTTP method.

A type guard that determines if the status code is informational.

A type guard that determines if the status code is a redirection.

A type guard that determines if the status code is a server error.

A type guard that determines if the status code is successful.

Allows merging of various sources of headers into a final set of headers which can be used in a Response.

Serves HTTP requests with the given handler.

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

Serves HTTPS requests with the given handler.

Set the cookie header properly in the headers

Interfaces

Information about the connection a request arrived on.

Provides a iterable map interfaces for managing cookies server side.

Just the part of Deno.FileInfo that is required to calculate an ETag, so partial or user generated file information can be passed.

An object which contains a headers property which has a value of an instance of Headers, like Request and Response.

An interface which describes the methods that SecureCookieMap uses to sign and verify cookies.

Additional serve options.

Options for running an HTTP server.

Type Aliases

An HTTP status that is a client error (4XX).

Types of data that can be signed cryptographically.

An HTTP status that is an error (4XX and 5XX).

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

A type representing string literals of each of the common HTTP method.

An HTTP status that is a informational (1XX).

An HTTP status that is a redirect (3XX).

An HTTP status that is a server error (5XX).

An HTTP status that is a success (2XX).