Skip to content

Commit 75295a0

Browse files
feat: Allow setting bound host in serve with -H (#231)
Co-authored-by: Luffy <[email protected]>
1 parent 55234bd commit 75295a0

File tree

7 files changed

+26
-4
lines changed

7 files changed

+26
-4
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,11 @@ docsify serve [path] [--open false] [--port 3000]
9898
- Type: number
9999
- Default: `3000`
100100
- Description: Choose a listen port, defaults to `3000`.
101+
- `--host` option:
102+
- Shorthand: `-H`
103+
- Type: string
104+
- Default: `localhost`
105+
- Description: Choose a host to bind to, defaults to `localhost`.
101106

102107
### `generate` command
103108

docs/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,11 @@ docsify serve [path] [--open false] [--port 3000]
8383
- Type: number
8484
- Default: `3000`
8585
- Description: Choose a listen port, defaults to `3000`.
86+
- `--host` option:
87+
- Shorthand: `-H`
88+
- Type: string
89+
- Default: `localhost`
90+
- Description: Choose a host to bind to, defaults to `localhost`.
8691

8792
### `generate` command
8893

lib/cli.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,14 @@ require('yargs')
8080
requiresArg: true,
8181
type: 'number'
8282
},
83+
host: {
84+
alias: 'H',
85+
default: 'localhost',
86+
desc: chalk.gray(y18n.__('serve.host')),
87+
nargs: 1,
88+
requiresArg: true,
89+
type: 'string'
90+
},
8391
'livereload-port': {
8492
alias: 'P',
8593
default: 35729,
@@ -96,7 +104,7 @@ require('yargs')
96104
type: 'string'
97105
}
98106
}),
99-
handler: argv => run.serve(argv.path, argv.open, argv.port, argv.P, argv.i)
107+
handler: argv => run.serve(argv.path, argv.open, argv.port, argv.host, argv.P, argv.i)
100108
})
101109
.command({
102110
command: 'generate [path]',

lib/commands/serve.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ module.exports = function (
1515
path,
1616
openInBrowser,
1717
port,
18+
host,
1819
livereloadPort,
1920
indexName
2021
) {
@@ -50,7 +51,7 @@ module.exports = function (
5051
)
5152
server.use(history({index: '/' + indexFileName}))
5253
server.use(serveStatic(path, {index: indexName}))
53-
server.listen(port)
54+
server.listen(port, host)
5455
lrserver
5556
.createServer({
5657
extraExts: ['md'],
@@ -60,15 +61,15 @@ module.exports = function (
6061
.watch(path)
6162

6263
if (openInBrowser) {
63-
open(`http://localhost:${port}`)
64+
open(`http://${host}:${port}`)
6465
}
6566

6667
const msg =
6768
'\nServing ' +
6869
chalk.green(`${path}`) +
6970
' now.\n' +
7071
'Listening at ' +
71-
chalk.green(`http://localhost:${port}`) +
72+
chalk.green(`http://${host}:${port}`) +
7273
'\n'
7374
console.log(msg)
7475
})

tools/locales/de.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
"serve": "Lasse lokalen Server zur Webseitenvorschau laufen.",
1010
"serve.open": "Dokumentation im Standardbrowser öffnen. Um explizit --open auf false zu setzen, kannst du --no-open verwenden.",
1111
"serve.port": "Listen port.",
12+
"serve.host": "Bind to host.",
1213
"serve.indexname": "Custom filename instead of index.html to serve by default",
1314
"livereload.port": "livereload Listen port.",
1415
"usage": "Anwendung",

tools/locales/en.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"serve": "Run local server to preview site.",
1111
"serve.open": "Open docs in default browser. To explicitly set --open to false you may use --no-open.",
1212
"serve.port": "Listen port.",
13+
"serve.host": "Bind to host.",
1314
"serve.indexname": "Custom filename instead of index.html to serve by default",
1415
"generate": "Docsify's generators",
1516
"generate.sidebar": "Generate sidebar file",

tools/locales/zh.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"serve": "本地预览",
1111
"serve.open": "自动打开浏览器",
1212
"serve.port": "设置端口",
13+
"serve.host": "设置监听地址",
1314
"serve.indexname": "自定义入口文件名,代替默认的 index.html",
1415
"generate": "docsify 的生成器",
1516
"generate.sidebar": "生成侧边栏文件",

0 commit comments

Comments
 (0)