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 {}