From 176716bdafd0adf62043e4c6a4278a629b802e6f Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Mon, 17 Jan 2022 10:37:55 +0000 Subject: [PATCH] feat(nitro): ssl nitro support in production (#2742) --- packages/nitro/src/runtime/entries/server.ts | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/packages/nitro/src/runtime/entries/server.ts b/packages/nitro/src/runtime/entries/server.ts index 4bd5f92bb4..93709ca07e 100644 --- a/packages/nitro/src/runtime/entries/server.ts +++ b/packages/nitro/src/runtime/entries/server.ts @@ -1,9 +1,13 @@ import '#polyfill' -import { Server } from 'http' +import { Server as HttpServer } from 'http' +import { Server as HttpsServer } from 'https' import destr from 'destr' import { handle } from '../server' -const server = new Server(handle) +const cert = process.env.NITRO_SSL_CERT +const key = process.env.NITRO_SSL_KEY + +const server = cert && key ? new HttpsServer({ key, cert }, handle) : new HttpServer(handle) const port = (destr(process.env.NUXT_PORT || process.env.PORT) || 3000) as number const hostname = process.env.NUXT_HOST || process.env.HOST || 'localhost' @@ -14,7 +18,8 @@ server.listen(port, hostname, (err) => { console.error(err) process.exit(1) } - console.log(`Listening on http://${hostname}:${port}`) + const protocol = cert && key ? 'https' : 'http' + console.log(`Listening on ${protocol}://${hostname}:${port}`) }) export default {}