fix: host addr

This commit is contained in:
hypercross 2026-03-21 00:29:36 +08:00
parent 7a490b82df
commit 3d9617f06c
3 changed files with 9 additions and 4 deletions

View File

@ -204,6 +204,7 @@ export function createContentServer(
contentDir: string, contentDir: string,
port: number, port: number,
distPath: string = distDir, distPath: string = distDir,
host: string = "0.0.0.0",
): ContentServer { ): ContentServer {
let contentIndex: ContentIndex = {}; let contentIndex: ContentIndex = {};
@ -263,11 +264,12 @@ export function createContentServer(
// 创建 HTTP 服务器 // 创建 HTTP 服务器
const server = createServer(handleRequest); const server = createServer(handleRequest);
server.listen(port, () => { server.listen(port, host, () => {
console.log(`\n开发服务器已启动http://localhost:${port}`); const displayHost = host === "0.0.0.0" ? "localhost" : host;
console.log(`\n开发服务器已启动http://${displayHost}:${port}`);
console.log(`内容目录:${contentDir}`); console.log(`内容目录:${contentDir}`);
console.log(`静态资源目录:${distPath}`); console.log(`静态资源目录:${distPath}`);
console.log(`索引文件http://localhost:${port}/__CONTENT_INDEX.json\n`); console.log(`索引文件http://${displayHost}:${port}/__CONTENT_INDEX.json\n`);
}); });
return { return {
@ -288,6 +290,7 @@ export function createContentServer(
export const serveCommand: ServeCommandHandler = async (dir, options) => { export const serveCommand: ServeCommandHandler = async (dir, options) => {
const contentDir = resolve(dir); const contentDir = resolve(dir);
const port = parseInt(options.port, 10); const port = parseInt(options.port, 10);
const host = options.host || "0.0.0.0";
createContentServer(contentDir, port); createContentServer(contentDir, port, distDir, host);
}; };

View File

@ -17,6 +17,7 @@ program
.description('运行一个 web 服务器预览目录中的内容,并实时监听更新') .description('运行一个 web 服务器预览目录中的内容,并实时监听更新')
.argument('[dir]', '要预览的目录', '.') .argument('[dir]', '要预览的目录', '.')
.option('-p, --port <port>', '端口号', '3000') .option('-p, --port <port>', '端口号', '3000')
.option('-h, --host <host>', '主机地址', '0.0.0.0')
.action(serveCommand); .action(serveCommand);
program program

View File

@ -1,5 +1,6 @@
export interface ServeOptions { export interface ServeOptions {
port: string; port: string;
host?: string;
} }
export interface CompileOptions { export interface CompileOptions {