Skip to content

Commit

Permalink
feat(server): allow specifying custom hostname (#377)
Browse files Browse the repository at this point in the history
  • Loading branch information
brettpappas authored Jun 18, 2020
1 parent 5ee1d15 commit fe9b04e
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 3 deletions.
8 changes: 5 additions & 3 deletions src/node/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,9 @@ async function runServe(options: UserConfig) {
const server = require('./server').createServer(options)

let port = options.port || 3000
let hostname = options.hostname || 'localhost'
const protocol = options.https ? 'https' : 'http'

server.on('error', (e: Error & { code?: string }) => {
if (e.code === 'EADDRINUSE') {
console.log(`Port ${port} is in use, trying another one...`)
Expand All @@ -139,7 +141,7 @@ async function runServe(options: UserConfig) {
}
})

server.listen(port, () => {
server.listen(port, hostname, () => {
console.log()
console.log(` Dev server running at:`)
const interfaces = os.networkInterfaces()
Expand All @@ -151,7 +153,7 @@ async function runServe(options: UserConfig) {
type: detail.address.includes('127.0.0.1')
? 'Local: '
: 'Network: ',
host: detail.address.replace('127.0.0.1', 'localhost')
host: detail.address.replace('127.0.0.1', hostname)
}
})
.forEach(({ type, host }) => {
Expand All @@ -164,7 +166,7 @@ async function runServe(options: UserConfig) {

if (options.open) {
require('./utils/openBrowser').openBrowser(
`${protocol}://localhost:${port}`
`${protocol}://${hostname}:${port}`
)
}
})
Expand Down
1 change: 1 addition & 0 deletions src/node/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ export interface SharedConfig {
}

export interface ServerConfig extends SharedConfig {
hostname?: string
port?: number
open?: boolean
/**
Expand Down

0 comments on commit fe9b04e

Please sign in to comment.