diff --git a/src/cli/commands/serve.ts b/src/cli/commands/serve.ts index 680ab3c..46a3001 100644 --- a/src/cli/commands/serve.ts +++ b/src/cli/commands/serve.ts @@ -204,6 +204,7 @@ export function createContentServer( contentDir: string, port: number, distPath: string = distDir, + host: string = "0.0.0.0", ): ContentServer { let contentIndex: ContentIndex = {}; @@ -263,11 +264,12 @@ export function createContentServer( // 创建 HTTP 服务器 const server = createServer(handleRequest); - server.listen(port, () => { - console.log(`\n开发服务器已启动:http://localhost:${port}`); + server.listen(port, host, () => { + const displayHost = host === "0.0.0.0" ? "localhost" : host; + console.log(`\n开发服务器已启动:http://${displayHost}:${port}`); console.log(`内容目录:${contentDir}`); console.log(`静态资源目录:${distPath}`); - console.log(`索引文件:http://localhost:${port}/__CONTENT_INDEX.json\n`); + console.log(`索引文件:http://${displayHost}:${port}/__CONTENT_INDEX.json\n`); }); return { @@ -288,6 +290,7 @@ export function createContentServer( export const serveCommand: ServeCommandHandler = async (dir, options) => { const contentDir = resolve(dir); const port = parseInt(options.port, 10); + const host = options.host || "0.0.0.0"; - createContentServer(contentDir, port); + createContentServer(contentDir, port, distDir, host); }; diff --git a/src/cli/index.ts b/src/cli/index.ts index 942a0bf..4f10856 100644 --- a/src/cli/index.ts +++ b/src/cli/index.ts @@ -17,6 +17,7 @@ program .description('运行一个 web 服务器预览目录中的内容,并实时监听更新') .argument('[dir]', '要预览的目录', '.') .option('-p, --port ', '端口号', '3000') + .option('-h, --host ', '主机地址', '0.0.0.0') .action(serveCommand); program diff --git a/src/cli/types.ts b/src/cli/types.ts index deab7c8..f323a8b 100644 --- a/src/cli/types.ts +++ b/src/cli/types.ts @@ -1,5 +1,6 @@ export interface ServeOptions { port: string; + host?: string; } export interface CompileOptions {