mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-30 09:27:13 +00:00
feat: support server
option in nuxt.config.js
to set PORT and HOST (#3701)
* feat: support `server` option in `nuxt.config.js` to set PORT and HOST * lint * change the conifg priority to `argv > nuxt.config.js > env.NUXT_PORT > env.PORT > package.json > default` * check for `options.server`
This commit is contained in:
parent
d73688292e
commit
0dff1b8fe9
@ -43,17 +43,12 @@ exports.loadNuxtConfig = (argv) => {
|
|||||||
options.mode =
|
options.mode =
|
||||||
(argv.spa && 'spa') || (argv.universal && 'universal') || options.mode
|
(argv.spa && 'spa') || (argv.universal && 'universal') || options.mode
|
||||||
|
|
||||||
|
// Server options
|
||||||
|
if (!options.server) {
|
||||||
|
options.server = {}
|
||||||
|
}
|
||||||
|
options.server.port = argv.port || options.server.port
|
||||||
|
options.server.host = argv.hostname || options.server.host
|
||||||
|
|
||||||
return options
|
return options
|
||||||
}
|
}
|
||||||
|
|
||||||
exports.getLatestHost = (argv) => {
|
|
||||||
const port =
|
|
||||||
argv.port || process.env.NUXT_PORT || process.env.PORT || process.env.npm_package_config_nuxt_port
|
|
||||||
const host =
|
|
||||||
argv.hostname ||
|
|
||||||
process.env.NUXT_HOST ||
|
|
||||||
process.env.HOST ||
|
|
||||||
process.env.npm_package_config_nuxt_host
|
|
||||||
|
|
||||||
return { port, host }
|
|
||||||
}
|
|
||||||
|
@ -3,7 +3,7 @@ const parseArgs = require('minimist')
|
|||||||
const consola = require('consola')
|
const consola = require('consola')
|
||||||
const { version } = require('../package.json')
|
const { version } = require('../package.json')
|
||||||
const { Nuxt, Builder } = require('..')
|
const { Nuxt, Builder } = require('..')
|
||||||
const { loadNuxtConfig, getLatestHost } = require('./common/utils')
|
const { loadNuxtConfig } = require('./common/utils')
|
||||||
|
|
||||||
const argv = parseArgs(process.argv.slice(2), {
|
const argv = parseArgs(process.argv.slice(2), {
|
||||||
alias: {
|
alias: {
|
||||||
@ -54,9 +54,6 @@ let hooked = false
|
|||||||
let dev = startDev()
|
let dev = startDev()
|
||||||
|
|
||||||
function startDev(oldInstance) {
|
function startDev(oldInstance) {
|
||||||
// Get latest environment variables
|
|
||||||
const { port, host } = getLatestHost(argv)
|
|
||||||
|
|
||||||
// Error handler
|
// Error handler
|
||||||
const onError = (err, instance) => {
|
const onError = (err, instance) => {
|
||||||
consola.error(err)
|
consola.error(err)
|
||||||
@ -71,6 +68,9 @@ function startDev(oldInstance) {
|
|||||||
return onError(err, oldInstance)
|
return onError(err, oldInstance)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Get latest environment variables
|
||||||
|
const { port, host } = options.server
|
||||||
|
|
||||||
// Create nuxt and builder instance
|
// Create nuxt and builder instance
|
||||||
let nuxt
|
let nuxt
|
||||||
let builder
|
let builder
|
||||||
|
@ -4,7 +4,7 @@ const { resolve } = require('path')
|
|||||||
const parseArgs = require('minimist')
|
const parseArgs = require('minimist')
|
||||||
const consola = require('consola')
|
const consola = require('consola')
|
||||||
const { Nuxt } = require('../dist/nuxt-start')
|
const { Nuxt } = require('../dist/nuxt-start')
|
||||||
const { loadNuxtConfig, getLatestHost } = require('./common/utils')
|
const { loadNuxtConfig } = require('./common/utils')
|
||||||
|
|
||||||
const argv = parseArgs(process.argv.slice(2), {
|
const argv = parseArgs(process.argv.slice(2), {
|
||||||
alias: {
|
alias: {
|
||||||
@ -76,7 +76,7 @@ if (nuxt.options.render.ssr === true) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const { port, host } = getLatestHost(argv)
|
const { port, host } = options.server
|
||||||
|
|
||||||
nuxt.listen(port, host).then(() => {
|
nuxt.listen(port, host).then(() => {
|
||||||
nuxt.showReady(false)
|
nuxt.showReady(false)
|
||||||
|
3
examples/custom-port-host/README.md
Normal file
3
examples/custom-port-host/README.md
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
# Custom PORT and HOST in `nuxt.config.js` with Nuxt.js
|
||||||
|
|
||||||
|
https://nuxtjs.org/examples/custom-port-host
|
6
examples/custom-port-host/nuxt.config.js
Normal file
6
examples/custom-port-host/nuxt.config.js
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
export default {
|
||||||
|
server: {
|
||||||
|
port: 8000,
|
||||||
|
host: '0.0.0.0'
|
||||||
|
}
|
||||||
|
}
|
11
examples/custom-port-host/package.json
Normal file
11
examples/custom-port-host/package.json
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
{
|
||||||
|
"name": "example-custom-port-host",
|
||||||
|
"dependencies": {
|
||||||
|
"nuxt": "latest"
|
||||||
|
},
|
||||||
|
"scripts": {
|
||||||
|
"dev": "nuxt",
|
||||||
|
"build": "nuxt build",
|
||||||
|
"start": "nuxt start"
|
||||||
|
}
|
||||||
|
}
|
3
examples/custom-port-host/pages/index.vue
Normal file
3
examples/custom-port-host/pages/index.vue
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
<template>
|
||||||
|
<p>Set port and host in <code>nuxt.config.js</code>.</p>
|
||||||
|
</template>
|
@ -14,6 +14,16 @@ export default {
|
|||||||
// Mode
|
// Mode
|
||||||
mode: 'universal',
|
mode: 'universal',
|
||||||
|
|
||||||
|
// Server options
|
||||||
|
server: {
|
||||||
|
port: process.env.NUXT_PORT ||
|
||||||
|
process.env.PORT ||
|
||||||
|
process.env.npm_package_config_nuxt_port,
|
||||||
|
host: process.env.NUXT_HOST ||
|
||||||
|
process.env.HOST ||
|
||||||
|
process.env.npm_package_config_nuxt_host
|
||||||
|
},
|
||||||
|
|
||||||
// Dirs
|
// Dirs
|
||||||
buildDir: '.nuxt',
|
buildDir: '.nuxt',
|
||||||
nuxtDir,
|
nuxtDir,
|
||||||
|
4
test/fixtures/with-config/nuxt.config.js
vendored
4
test/fixtures/with-config/nuxt.config.js
vendored
@ -2,6 +2,10 @@ import path from 'path'
|
|||||||
|
|
||||||
export default {
|
export default {
|
||||||
srcDir: __dirname,
|
srcDir: __dirname,
|
||||||
|
server: {
|
||||||
|
port: 8000,
|
||||||
|
host: '0.0.0.0'
|
||||||
|
},
|
||||||
router: {
|
router: {
|
||||||
base: '/test/',
|
base: '/test/',
|
||||||
middleware: 'noop',
|
middleware: 'noop',
|
||||||
|
Loading…
Reference in New Issue
Block a user