1. 程式人生 > >一個原生的靜態html專案需要前後端互動或測試的時候怎麼辦呢?(在本地開啟http服務)

一個原生的靜態html專案需要前後端互動或測試的時候怎麼辦呢?(在本地開啟http服務)

我們做專案的時候有時用webpack ,glup,什麼的都會生成一個本地地址,但是原生的沒有,這時候當你需要前後端互動,或者測試說我們連你IP在你本地先測吧!這時候該怎麼辦呢,不可能臨時換成vue,或者其他的沒有必要。於是問了一下別人叫了我一個很使用的方法!

要像使用這個方法前提是你已經安裝了node.js。

http-server 是一個簡單的零配置命令列HTTP伺服器, 基於 nodeJs.

安裝 (全域性安裝加 -g) : 

 npm install http-server 

Windows 下使用:

在站點目錄下開啟命令列輸入

 http-server

訪問: http://localhost:8080 or http://127.0.0.1:8080 

 

用法:

http-server [path] [options]

[path]預設情況下,./public如果資料夾存在,./否則。(一般直接在專案中輸入http-server就可以了,)

現在您可以訪問http:// localhost:8080來檢視您的伺服器

 

引數 :

-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 列印以上列表並退出