jayKumar
jayKumar

Reputation: 151

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

Upvotes: 15

Views: 6710

Answers (7)

Kevin Qian
Kevin Qian

Reputation: 2720

Using Deno.serve:

As shown in the Deno.serve documentation:

const options = {
  port: 443,
  cert: await Deno.readTextFile("./cert.pem"),
  key: await Deno.readTextFile("./key.pem"),
};

Deno.serve(options, (request) => {
  const body = `Your user-agent is: ${request.headers.get("user-agent")}`;
  return new Response(body, { status: 200 });
});

You can generate the cert and key files like this on Linux, for example:

openssl req -x509 -newkey rsa:4096 -keyout key.pem -out cert.pem -days 365 -nodes -subj '/CN=localhost'

Old Answer (Deprecated):

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 });
}

Upvotes: 13

Sandeep Patel
Sandeep Patel

Reputation: 5148

Now TLS binding is supported by Deno. 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:

Upvotes: 0

fwqaaq
fwqaaq

Reputation: 1

bro, I run into some situations when I code like as follow:

/** @format */
const cert = Deno.readTextFileSync('./cert.pem')
const key = Deno.readTextFileSync('./private.pem')

const listener = Deno.listenTls({
  cert,
  key,
  hostname: 'localhost',
  port: 8080,
})

console.log('Server running on https://localhost:8080/')

for await (const conn of listener) {
  handleConn(conn)
}

async function handleConn(conn: Deno.TlsConn) {
  const httpConn = Deno.serveHttp(conn)

  for await (const req of httpConn) {
    const url = new URL(req.request.url)
    if (url.pathname === '/favicon.ico') continue
    const path = url.pathname === '/' ? '/welcome.html' : url.pathname
    const ext = path.split('.').pop()

    const file = (await Deno.open(`./http/example${path}`)).readable
    let res: Response | null = null
    switch (ext) {
      case 'html' || 'css':
        res = resBuilder(file, `text/${ext}`)
        break
      case 'js':
        res = resBuilder(file, 'text/javascript')
        break
      case 'png' || 'jpg' || 'ico':
        res = resBuilder(file, `image/${ext}`)
        break
      default:
        res = resBuilder(file, '*/*')
    }
    req.respondWith(res!)
  }
}

function resBuilder(data: ReadableStream<Uint8Array>, contentType: string) {
  return new Response(data, {
    headers: new Headers({ 'content-type': contentType }),
  })
}

When I opened it in the browser, an error occurred:

error: Uncaught (in promise) Http: error writing a body to connection: tls handshake eof: tls handshake eof
  for await (const req of httpConn) {
                   ^
    at async HttpConn.nextRequest (ext:deno_http/01_http.js:101:21)
    at async Object.next (ext:deno_http/01_http.js:184:24)
    at async handleConn (file:///Users/feiwu/Project/node/coding_and_nas/http/example/http.ts:24:20)

Upvotes: 0

Zwiers
Zwiers

Reputation: 3658

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);
    }
  }
}

Upvotes: 1

Subhash Malireddy
Subhash Malireddy

Reputation: 23

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.

Upvotes: 0

Songwon Park
Songwon Park

Reputation: 135

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.

Upvotes: 0

Speedyankur
Speedyankur

Reputation: 147

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");

Upvotes: -3

Related Questions