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来搞定。
http-server好处:
- 可以使静态资源文件的任意一个目录成为服务器的目录,完全抛开后台的沉重工作,直接运行你想要的代码。
http-server安装及运行:
- 安装node.js
- 使用npm/cnpm安装http-server;运行 npm install http-server -g
- 开始使用:使用cd跳转到你想要查看的文件夹下面,例:cd dist;然后将文件夹放到本地服务器上。使用http-server 启动;然后会出现以下代码:
浏览器使用 http://127.0.0.1:8081 或 http://10.1.1.232:8081启动即可查看你想运行的代码;Starting up http-server, serving ./ Available on: http://127.0.0.1:8081 http://10.1.1.232:8081 Hit CTRL-C to stop the server - 当不使用时,直接使用 ctrl+c 结束即可;
http-server可用参数( Available Options ):
-pPort to use (defaults to 8080)-aAddress to use (defaults to 0.0.0.0)-dShow directory listings (defaults to 'True')-iDisplay autoIndex (defaults to 'True')-gor--gzipWhen enabled (defaults to 'False') it will serve./public/some-file.js.gzin place of./public/some-file.jswhen a gzipped version of the file exists and the request accepts gzip encoding.-eor--extDefault file extension if none supplied (defaults to 'html')-sor--silentSuppress log messages from output--corsEnable CORS via theAccess-Control-Allow-Originheader-oOpen browser window after starting the server-cSet 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.-Uor--utcUse UTC time format in log messages.-Por--proxyProxies all requests which can't be resolved locally to the given url. e.g.: -P http://someurl.com-Sor--sslEnable https-Cor--certPath to ssl cert file (default: cert.pem).-Kor--keyPath to ssl key file (default: key.pem).-ror--robotsProvide a /robots.txt (whose content defaults to 'User-agent: *\nDisallow: /')-hor--helpPrint this list and exit.
本文介绍了一个轻量级HTTP服务器——http-server的使用方法。适用于前端开发者在开发过程中快速搭建服务器,用于测试静态文件,避免MIME类型错误。文章详细解释了如何安装、配置并运行http-server,以及其常用参数。
707

被折叠的 条评论
为什么被折叠?



