ZidanZ
Deno3y ago
167 replies
Zidan

How to serve HTTPS with the Deno.serve() API

I used to start a dev server with TLS like so

const server = Deno.listenTls({
  port: 7000,
  transport: 'tcp',
  hostname: 'localhost',
  certFile: './server/ssl/server.crt',
  keyFile: './server/ssl/server.key',
  alpnProtocols: ['h2', 'http/1.1']
})

const router = async (connection) => {

  const httpConnection = Deno.serveHttp(connection)

  for await (const event of httpConnection) {

    const url = new URL(event.request.url)
    const path = url.pathname
    const cookie = event.request.headers.get('cookie')
    
    if (path === '/') {
      await event.respondWith(
        handlers.main({ cookie })
      )
    }

  }
}

for await (const connection of server) {
  router(connection)
}


but can't seem to do so with the Deno.serve() api

const options = {
  port: 7000,
  hostname: "localhost",
  certFile: "./server/ssl/server.crt",
  keyFile: "./server/ssl/server.key",
  alpnProtocols: ['h2', 'http/1.1']
}

const handler = (req) => { return new Response("what's up") }

Deno.serve(options, handler)


It seems to me that the cert and key files in the options object are being ignored and i have no idea why.
Any help is greatly appreciated.
Was this page helpful?