From eb1e25ba24341bb037cce56638684df9012956d2 Mon Sep 17 00:00:00 2001 From: Liu Xiaoyi Date: Fri, 17 Oct 2025 07:26:32 +0800 Subject: [PATCH 1/4] feat: Allow setting bound host in serve with `-H` --- lib/cli.js | 10 +++++++++- lib/commands/serve.js | 7 ++++--- tools/locales/de.json | 1 + tools/locales/en.json | 1 + tools/locales/zh.json | 1 + 5 files changed, 16 insertions(+), 4 deletions(-) diff --git a/lib/cli.js b/lib/cli.js index 09d5fd8..3b0d403 100644 --- a/lib/cli.js +++ b/lib/cli.js @@ -80,6 +80,14 @@ require('yargs') requiresArg: true, type: 'number' }, + host: { + alias: 'H', + default: 'localhost', + desc: chalk.gray(y18n.__('serve.host')), + nargs: 1, + requiresArg: true, + type: 'string' + }, 'livereload-port': { alias: 'P', default: 35729, @@ -96,7 +104,7 @@ require('yargs') type: 'string' } }), - handler: argv => run.serve(argv.path, argv.open, argv.port, argv.P, argv.i) + handler: argv => run.serve(argv.path, argv.open, argv.port, argv.host, argv.P, argv.i) }) .command({ command: 'generate [path]', diff --git a/lib/commands/serve.js b/lib/commands/serve.js index 2e5155d..d14458f 100644 --- a/lib/commands/serve.js +++ b/lib/commands/serve.js @@ -15,6 +15,7 @@ module.exports = function ( path, openInBrowser, port, + host, livereloadPort, indexName ) { @@ -50,7 +51,7 @@ module.exports = function ( ) server.use(history({index: '/' + indexFileName})) server.use(serveStatic(path, {index: indexName})) - server.listen(port) + server.listen(port, host) lrserver .createServer({ extraExts: ['md'], @@ -60,7 +61,7 @@ module.exports = function ( .watch(path) if (openInBrowser) { - open(`http://localhost:${port}`) + open(`http://${host}:${port}`) } const msg = @@ -68,7 +69,7 @@ module.exports = function ( chalk.green(`${path}`) + ' now.\n' + 'Listening at ' + - chalk.green(`http://localhost:${port}`) + + chalk.green(`http://${host}:${port}`) + '\n' console.log(msg) }) diff --git a/tools/locales/de.json b/tools/locales/de.json index 611c8ec..d044a95 100644 --- a/tools/locales/de.json +++ b/tools/locales/de.json @@ -9,6 +9,7 @@ "serve": "Lasse lokalen Server zur Webseitenvorschau laufen.", "serve.open": "Dokumentation im Standardbrowser öffnen. Um explizit --open auf false zu setzen, kannst du --no-open verwenden.", "serve.port": "Listen port.", + "serve.host": "Bind to host.", "serve.indexname": "Custom filename instead of index.html to serve by default", "livereload.port": "livereload Listen port.", "usage": "Anwendung", diff --git a/tools/locales/en.json b/tools/locales/en.json index cc8da7a..cdd3dc4 100644 --- a/tools/locales/en.json +++ b/tools/locales/en.json @@ -10,6 +10,7 @@ "serve": "Run local server to preview site.", "serve.open": "Open docs in default browser. To explicitly set --open to false you may use --no-open.", "serve.port": "Listen port.", + "serve.host": "Bind to host.", "serve.indexname": "Custom filename instead of index.html to serve by default", "generate": "Docsify's generators", "generate.sidebar": "Generate sidebar file", diff --git a/tools/locales/zh.json b/tools/locales/zh.json index 8d00fa3..049def3 100644 --- a/tools/locales/zh.json +++ b/tools/locales/zh.json @@ -10,6 +10,7 @@ "serve": "本地预览", "serve.open": "自动打开浏览器", "serve.port": "设置端口", + "serve.host": "设置监听地址", "serve.indexname": "自定义入口文件名,代替默认的 index.html", "generate": "docsify 的生成器", "generate.sidebar": "生成侧边栏文件", From 7be3806a00ee5ce48cb811d319c551153bce98cd Mon Sep 17 00:00:00 2001 From: Liu Xiaoyi Date: Fri, 17 Oct 2025 16:35:59 +0800 Subject: [PATCH 2/4] chore: Add documentation about --- README.md | 5 +++++ docs/README.md | 5 +++++ 2 files changed, 10 insertions(+) diff --git a/README.md b/README.md index 9c2d8f6..06ec86a 100644 --- a/README.md +++ b/README.md @@ -98,6 +98,11 @@ docsify serve [path] [--open false] [--port 3000] - Type: number - Default: `3000` - Description: Choose a listen port, defaults to `3000`. +- `--host` option: + - Shorthand: `-H` + - Type: string + - Default: `"localhost"` + - Description: Choose a host to bind to, defaults to `"localhost"`. ### `generate` command diff --git a/docs/README.md b/docs/README.md index 3f2fc4e..00ea217 100644 --- a/docs/README.md +++ b/docs/README.md @@ -83,6 +83,11 @@ docsify serve [path] [--open false] [--port 3000] - Type: number - Default: `3000` - Description: Choose a listen port, defaults to `3000`. +- `--host` option: + - Shorthand: `-H` + - Type: string + - Default: `"localhost"` + - Description: Choose a host to bind to, defaults to `"localhost"`. ### `generate` command From ea725bfe6156be1302155419c0c01a9c09e96da0 Mon Sep 17 00:00:00 2001 From: Luffy <52o@qq52o.cn> Date: Fri, 17 Oct 2025 16:53:46 +0800 Subject: [PATCH 3/4] Apply suggestion from @sy-records --- docs/README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/README.md b/docs/README.md index 00ea217..c0e4466 100644 --- a/docs/README.md +++ b/docs/README.md @@ -86,8 +86,8 @@ docsify serve [path] [--open false] [--port 3000] - `--host` option: - Shorthand: `-H` - Type: string - - Default: `"localhost"` - - Description: Choose a host to bind to, defaults to `"localhost"`. + - Default: `localhost` + - Description: Choose a host to bind to, defaults to `localhost`. ### `generate` command From a3240c789e2a673e65ac4466ed9d7786e0b35dcc Mon Sep 17 00:00:00 2001 From: Luffy <52o@qq52o.cn> Date: Fri, 17 Oct 2025 16:54:06 +0800 Subject: [PATCH 4/4] Apply suggestion from @sy-records --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 06ec86a..f44e221 100644 --- a/README.md +++ b/README.md @@ -101,8 +101,8 @@ docsify serve [path] [--open false] [--port 3000] - `--host` option: - Shorthand: `-H` - Type: string - - Default: `"localhost"` - - Description: Choose a host to bind to, defaults to `"localhost"`. + - Default: `localhost` + - Description: Choose a host to bind to, defaults to `localhost`. ### `generate` command