httpserver的使用

在写前端页面中,经常会在浏览器运行HTML页面,从本地文件夹中直接打开的一般都是file协议,当代码中存在http或https的链接时,HTML页面就无法正常打开,为了解决这种情况,需要在在本地开启一个本地的服务器。

  1. 安装
npm install http-server -g
  1. 使用
    在当前目录下的www启动服务器,端口为9000,启动成功后会打开默认浏览器,地址为:http://localhost:9000
http-server './apidoc/' -p 9000 -o
  1. http-server常用参数
  • -p 端口号 (默认 8080)
  • -a IP 地址 (默认 0.0.0.0)
  • -d 显示目录列表 (默认 ‘True’)
  • -i 显示 autoIndex (默认 ‘True’)
  • -e or --ext 如果没有提供默认的文件扩展名(默认 ‘html’)
  • -s or --silent 禁止日志信息输出
  • --cors 启用 CORS via the Access-Control-Allow-Origin header
  • -o 在开始服务后打开浏览器
  • -c 为 cache-control max-age header 设置Cache time(秒) , e.g. -c10 for 10 seconds (defaults to ‘3600’). 禁用 caching, 则使用 -c-1.
  • -U 或 --utc 使用UTC time 格式化log消息
  • -P or --proxy Proxies all requests which can’t be resolved locally to the given url. e.g.: -P http://someurl.com
  • -S or --ssl 启用 https
  • -C or --cert ssl cert 文件路径 (default: cert.pem)
  • -K or --key Path to ssl key file (default: key.pem).
  • -r or --robots Provide a /robots.txt (whose content defaults to ‘User-agent: *\nDisallow: /’)
  • -h or --help 打印以上列表并退出
  1. 关闭 http-server服务
    按快捷键CTRL+C
    终端显示^Chttp-server stopped.即关闭服务成功。
砥砺前行
原文地址:https://www.cnblogs.com/lhongsen/p/14753175.html