【Node.js】http-server 实现目录浏览服务

前言

  • node.js v12.18.2
  • 用 node.js 实现目录浏览服务
  • 前提,已经按照好Node.js
  • http-server 官网:https://www.npmjs.com/package/http-server

下载安装http-server

shell> npm install http-server -g
npm WARN dirservice@1.0.0 No description
npm WARN dirservice@1.0.0 No repository field.

+ http-server@0.12.3
added 32 packages from 39 contributors and audited 32 packages in 42.284s

2 packages are looking for funding
  run `npm fund` for details

found 0 vulnerabilities

启动http-server

命令

http-server [path] [options]

[path]的默认值:先找 ./public ,否则使用 ./

例如:

shell> http-server
Starting up http-server, serving ./
Available on:
  http://192.168.0.165:8080
  http://127.0.0.1:8080
Hit CTRL-C to stop the server

效果如下:
在这里插入图片描述

更多设置

帮助

shell> http-server --help
usage: http-server [path] [options]

options:
  -p --port    Port to use [8080]
  -a           Address to use [0.0.0.0]
  -d           Show directory listings [true]
  -i           Display autoIndex [true]
  -g --gzip    Serve gzip files when possible [false]
  -b --brotli  Serve brotli files when possible [false]
               If both brotli and gzip are enabled, brotli takes precedence
  -e --ext     Default file extension if none supplied [none]
  -s --silent  Suppress log messages from output
  --cors[=headers]   Enable CORS via the "Access-Control-Allow-Origin" header
                     Optionally provide CORS headers list separated by commas
  -o [path]    Open browser window after starting the server.
               Optionally provide a URL path to open the browser window to.
  -c           Cache time (max-age) in seconds [3600], e.g. -c10 for 10 seconds.
               To disable caching, use -c-1.
  -t           Connections timeout in seconds [120], e.g. -t60 for 1 minute.
               To disable timeout, use -t0
  -U --utc     Use UTC time format in log messages.
  --log-ip     Enable logging of the client's IP address

  -P --proxy         Fallback proxy if the request cannot be resolved. e.g.: http://someurl.com

  --username   Username for basic authentication [none]
               Can also be specified with the env variable NODE_HTTP_SERVER_USERNAME
  --password   Password for basic authentication [none]
               Can also be specified with the env variable NODE_HTTP_SERVER_PASSWORD

  -S --ssl     Enable https.
  -C --cert    Path to ssl cert file (default: cert.pem).
  -K --key     Path to ssl key file (default: key.pem).

  -r --robots        Respond to /robots.txt [User-agent: *\nDisallow: /]
  --no-dotfiles      Do not show dotfiles
  -h --help          Print this list and exit.
  -v --version       Print the version and exit.

或者:
参考官网介绍

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值