Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add https capabilities to dev server by adding https in nuxt config. #3403

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions bin/nuxt-dev
Expand Up @@ -111,7 +111,7 @@ function startDev(oldInstance) {
if (oldInstance && oldInstance.builder) {
return oldInstance.builder.unwatch()
} else {
return nuxt.listen(port, host)
return nuxt.listen(port, host, nuxtConfig.https)
Copy link
Member

@clarkdo clarkdo Aug 1, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe unnecessary to pass https, config can be fetched through this.options inside nuxt.js

}
})
// Start build
Expand All @@ -126,7 +126,7 @@ function startDev(oldInstance) {
// Start listening
.then(() => {
if (oldInstance) {
return nuxt.listen(port, host)
return nuxt.listen(port, host, nuxtConfig.https)
} else {
return Promise.resolve()
}
Expand Down
16 changes: 13 additions & 3 deletions lib/core/nuxt.js
@@ -1,5 +1,6 @@
import Module from 'module'
import { resolve, join } from 'path'
import https from 'https'

import enableDestroy from 'server-destroy'
import _ from 'lodash'
Expand Down Expand Up @@ -119,9 +120,17 @@ export default class Nuxt {
})
}

listen(port = 3000, host = 'localhost') {
listen(port = 3000, host = 'localhost', httpsOptions = {}) {
const isHttps = httpsOptions === true || Object.keys(httpsOptions).length > 0
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think only check httpsOptions is not falsy is fine. An empty object also means enabling https, right?

let appServer

if (isHttps) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This part can be moved into below promise.

appServer = https.createServer(httpsOptions === true ? {} : httpsOptions, this.renderer.app)
} else {
appServer = this.renderer.app
}
return this.ready().then(() => new Promise((resolve, reject) => {
const server = this.renderer.app.listen(
const server = appServer.listen(
{ port, host, exclusive: false },
err => {
/* istanbul ignore if */
Expand All @@ -134,7 +143,8 @@ export default class Nuxt {
host = 'localhost'
}

const listenURL = chalk.underline.blue(`http://${host}:${port}`)
const listenURL = chalk.underline.blue(`http${isHttps ? 's' : ''}://${host}:${port}`)

this.readyMessage = `Listening on ${listenURL}`

// Close server on nuxt close
Expand Down