From fe9b04e818066c474c525f18dc7b4a2a05fe8ac1 Mon Sep 17 00:00:00 2001 From: Brett Pappas Date: Thu, 18 Jun 2020 17:43:43 -0400 Subject: [PATCH] feat(server): allow specifying custom hostname (#377) --- src/node/cli.ts | 8 +++++--- src/node/config.ts | 1 + 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/src/node/cli.ts b/src/node/cli.ts index 11b60489da0c07..f692d8376945c9 100644 --- a/src/node/cli.ts +++ b/src/node/cli.ts @@ -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...`) @@ -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() @@ -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 }) => { @@ -164,7 +166,7 @@ async function runServe(options: UserConfig) { if (options.open) { require('./utils/openBrowser').openBrowser( - `${protocol}://localhost:${port}` + `${protocol}://${hostname}:${port}` ) } }) diff --git a/src/node/config.ts b/src/node/config.ts index 23bf2be84394b2..253471ad5bfa45 100644 --- a/src/node/config.ts +++ b/src/node/config.ts @@ -119,6 +119,7 @@ export interface SharedConfig { } export interface ServerConfig extends SharedConfig { + hostname?: string port?: number open?: boolean /**