DenoDDeno
Powered by
ZidanZ
Deno•3y 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)
}
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)
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.
Deno banner
DenoJoin
Chat about Deno, a modern runtime for JavaScript and TypeScript.
20,944Members
Resources
Recent Announcements

Similar Threads

Was this page helpful?

Similar Threads

Deno.serve AbortController onError. How?
BairdyBBairdy / help
3y ago
deno serve logs?
Jack MJJack M / help
2y ago
How to set reuseaddr in `Deno.serve`?
CyanCCyan / help
15mo ago
Next page