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安裝及運行:
- 安裝node.js
- 使用npm/cnpm安裝http-server;運行 npm install http-server -g
- 開始使用:使用cd跳轉到你想要查看的文件夾下面,例:cd dist;然后將文件夾放到本地服務器上。使用http-server 啟動;然后會出現以下代碼:
-
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.
可選配置:
-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打印此列表並退出。
