My FeedDiscussionsHeadless CMS
New
Sign in
Log inSign up
Learn more about Hashnode Headless CMSHashnode Headless CMS
Collaborate seamlessly with Hashnode Headless CMS for Enterprise.
Upgrade ✨Learn more
Set HTTP Headers in Deno

Set HTTP Headers in Deno

Maxwell DeMers's photo
Maxwell DeMers
·Nov 19, 2020·

2 min read

Deno's runtime provides a host of functionality. If you haven't looked, check out the docs here.

One of the classes that is exposed in the runtime is Headers.

To start, first create a new Headers instance.

const headers = new Headers();

The Headers class can be instantiated without any arguments, or can be passed an object, or an array of tuples.

const headers = new Headers([ ["Cache-Control", "max-age=3600"] ]);
const headers = new Headers({ "Cache-Control": "max-age=3600" });

Once instantiated, you can manipulate or traverse the headers with delete, entries, has, etc. See the list of methods here.

You can see a working example of a server setting custom headers with the append method, below.

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

const server = serve({ hostname: "0.0.0.0", port: 8080 });

for await (const request of server) {
    const body = new TextEncoder().encode('<h1>Hello World</h1>\n <a href="http://localhost:8080/">Click me for a cached response</a>');

    const headers = new Headers({"Cache-Control": "max-age=3600"});

    headers.append("custom-header", "some value");

    request.respond({ status: 200, body, headers  });
}

If you'd like to watch a screencast on this, check it out here: