'Creating a Deno https server

I am looking for an example of creating a https server in Deno. I have seen examples of Deno http server but not https.

I have tried searching in google but found no results



Solution 1:[1]

serveTLS has landed along with Deno 0.23.0:

Sample usage:

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

const body = new TextEncoder().encode("Hello HTTPS");
const options = {
  hostname: "localhost",
  port: 443,
  certFile: "./path/to/localhost.crt",
  keyFile: "./path/to/localhost.key",
};
// Top-level await supported
for await (const req of serveTLS(options)) {
  req.respond({ body });
}

Solution 2:[2]

With Deno 1.9+ you could use the native server. It provides the ability to use HTTPS.

It is a good bit faster faster than older implementations of std/http. However, as of version 0.107.0 the native server is used for std/http as well.

Example:

const server = Deno.listenTls({
  port: 443,
  certFile: "./my-ca-certificate.pem",
  keyFile: "./my-key.pem"
});

for await (const conn of server) {
  handle(conn);
}

async function handle(conn: Deno.Conn) {
  const httpConn = Deno.serveHttp(conn);
  
  for await (const requestEvent of httpConn) {
    try {
      const response = new Response("Hello World!");
      await requestEvent.respondWith(response);
    } 
    catch (error) {
      console.error(error);
    }
  }
}

Solution 3:[3]

Now TLS binding is supported by Dano. Below are ways to create https server:

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

    const body = new TextEncoder().encode("Hello HTTPS");
    const options = {
      hostname: "localhost",
      port: 443,
      certFile: "./path/to/localhost.crt",
      keyFile: "./path/to/localhost.key",
    };

for await (const req of serveTLS(options)) {
  req.respond({ body });
}

serveTLS

Arguments options: any

return :Server

With listenAndServeTLS

listenAndServeTLS(options, (req) => {
   req.respond({ body });
 });

listenAndServeTLS

Arguments

  • options: any

  • handler: (req: ServerRequest) => void

return:any

For more details see official docs:

Solution 4:[4]

How about using deno oak framework?

https://github.com/oakserver/oak

I think the project is the most stable web framework in Deno. And you also get the much information from that you want to learn about it.

Solution 5:[5]

First of all, it is possible to create an HTTPS server with DENO std library. But in my case, I used the OAK library for my app. More about the oak library can be found here. Step-1: Have the certificate file and key file ready(assuming they are produced for whatever domain name you like. It could just be localhost). If you have no idea what this means, read this article.. Step-2: It's time to configure your app's listening options. You can copy the below line of code and change the paths to the certFile and keyFile options as necessary. More explanation given below.

await app.listen({ port: port, secure: true, certFile: "<path-to-file>/<file-name>.pem", keyFile: "<path-to-file>/<file-name>-key.pem" });

In case you want to know what's happening in the above line:

  1. Oak's Application's listen method accepts options to be configured and these options is of type ListenOptions which can either be ListenOptionsBase or ListenOptionsTls which are inherited from Deno.ListenOptions and Deno.ListenTlsOptions respectively. If you check the Deno.ListenTlsOptions there are two options which are certFile and keyFile which accepts paths to your certificate and key for the certificate respectively, which are both .pem files.

Solution 6:[6]

Have you checked DENO ABC? It is better framework to create web applications. Read more at https://deno.land/x/abc/README.md.

import { abc } from "https://deno.sh/abc/mod.ts";
const app = abc();
app
  .get("/hello", c => {
    return "Hello, Abc!";
  })
  .start("0.0.0.0:8080");

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Kevin Qian
Solution 2
Solution 3 Sandeep Patel
Solution 4 Songwon Park
Solution 5
Solution 6 David Medinets