http-server 基于nodejs的http服务器

 

http-server所用场景:

 

  • 作为前端的同学来说,想要运行一段代码,但又没有必要使用tomcat或是Apache http server,这个时候,一个简单的轻量的http-server就能搞定。
  • 当前端开发完成后,需要我们打包部署,此时一般就会生成一个dist文件夹,里面存放的是一些静态文件,当我们在编辑器里直接运行这些静态文件时,很可能会出现“because its MIME type ('text/html') is not a supported stylesheet MIME type, and strict MIME checking is enabled.”这类的关于MIME type的错误,这些错误是因为静态文件访问限制导致的,此时就可以使用http-server来搞定。
  • 在平常开发过程中,常遇到没有用任何框架和工具,就是要运行一个简单的html页面,需要启一个本地的服务器。
  • 有的时候做前端,想要运行一些代码,但是又没有必要使用tomcat或者Apache http server,这个时候一个轻量级的简单的http server就可以搞定了。

    Http-server是基于nodejs的http服务器,它最大好处就是:可以使任意一个目录成为服务器的目录,完全抛开后台的沉重工程,直接运行想要的js代码。

 

http-server好处:

 

  • 可以使静态资源文件的任意一个目录成为服务器的目录,完全抛开后台的沉重工作,直接运行你想要的代码。
http-server安装及运行:

 

  1. 安装node.js
  2. 使用npm/cnpm安装http-server;运行 npm install http-server -g
  3. 开始使用:使用cd跳转到你想要查看的文件夹下面,例:cd dist;然后将文件夹放到本地服务器上。使用http-server 启动;然后会出现以下代码:
    1.  
      Starting up http-server, serving ./
    2.  
      Available on:
    3.  
      http://127.0.0.1:8081
    4.  
      http://10.1.1.232:8081
    5.  
      Hit CTRL-C to stop the server
    浏览器使用 http://127.0.0.1:8081 或 http://10.1.1.232:8081启动即可查看你想运行的代码;
  4. 当不使用时,直接使用 ctrl+c 结束即可;
http-server可用参数( Available Options ):
  • -p Port to use (defaults to 8080)
  • -a Address to use (defaults to 0.0.0.0)
  • -d Show directory listings (defaults to 'True')
  • -i Display autoIndex (defaults to 'True')
  • -g or --gzip When enabled (defaults to 'False') it will serve ./public/some-file.js.gz in place of ./public/some-file.js when a gzipped version of the file exists and the request accepts gzip encoding.
  • -e or --ext Default file extension if none supplied (defaults to 'html')
  • -s or --silent Suppress log messages from output
  • --cors Enable CORS via the Access-Control-Allow-Origin header
  • -o Open browser window after starting the server
  • -c Set cache time (in seconds) for cache-control max-age header, e.g. -c10 for 10 seconds (defaults to '3600'). To disable caching, use -c-1.
  • -U or --utc Use UTC time format in log messages.
  • -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 Enable https
  • -C or --cert Path to ssl cert file (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 Print this list and exit.

 

可选配置: 

-p 要使用的端口(默认为8080)

-a 要使用的地址(默认为0.0.0.0)

-d 显示目录列表(默认为“True”)

-i 显示autoIndex(默认为“True”)

-g--gzip启用时(默认为“False”),它将用于./public/some-file.js.gz代替./public/some-file.jsgzip压缩版本的文件,并且该请求接受gzip编码。

-e--ext默认文件扩展名(如果没有提供)(默认为'html')

-s--silent从输出中抑制日志消息

--cors通过Access-Control-Allow-Origin标题启用CORS

-o 启动服务器后打开浏览器窗口

-c设置缓存控制max-age头的缓存时间(以秒为单位),例如-c10 10秒(默认为'3600')。要禁用缓存,请使用-c-1。

-U--utc在日志消息中使用UTC时间格式。

-P--proxy代理无法在本地解决给定网址的所有请求。例如:-P http://someurl.com

-S--ssl启用https。

-C--certssl证书文件的路径(默认值:cert.pem)。

-K--keyssl密钥文件的路径(默认值:key.pem)。

-r或者--robots提供一个/robots.txt(其内容默认为'User-agent:* \ nDisallow:/')

-h--help打印此列表并退出。

 

posted @ 2018-07-30 13:02  panchanggui  阅读(3226)  评论(0编辑  收藏  举报