Mac下安裝npm,http-server,安裝虛擬服務器


http-server是一個簡單的,不需要配置的命令行下使用的http服務器。類似的還有Xampp等。

針對前端開發工程的代碼不需要編譯的特點,使用這種簡單的服務器十分的便利。

 

1.安裝這個首先要安裝npm,所以npm是神馬?https://www.npmjs.com/

   npm是一個js、mobile等等的包管理工具,通過命令npm install 命令,可以很簡單方便的安裝各種軟件包,比如grunt、http-server等等

   我的安裝方式是下載node的dmg文件(https://nodejs.org/download/),下載完成后直接點擊安裝完成。

   node又是神馬?是JS運行平台。使用c++編寫而成,是一個js運行環境。

   上述操作完畢,可以在終端下檢驗是否node已經安裝完成。

~ xq$ node -v
v0.12.7

   ok~再檢查npm是不是自動安裝了

~ xq$ npm -v
2.11.3

good~可以使用npm安裝我們想要的http-server了

 

2. 執行簡單的命令行安裝http-server

~xq$ npm install http-server -g

-g 表示安裝在全局,不只是當前用戶能用。

如果你是按照鹵煮的步驟來得,應該會報錯 ··喵~ >▽< 

attention:如果報找不到文件的錯誤,請用管理員權限運行

~xq$ sudo npm install http-server -g

如果是安裝超時什么的,請更改配置文件。registry配置項默認配置的是國外網址,不翻牆是訪問不到的。so,改為圖中這個中國版的域名,或者使用淘寶的npm鏡像http://npm.taobao.org/

常見的npm的命令:
npm -v
npm -g install npm@2.9.1     2.9.1是版本號)
Npm config set registry http://…   設置鏡像
npm update -g npm

3.安裝完成了,開始使用http-server。

最簡單的使用方法,直接使用http-server,默認使用localhost和8080端口。在瀏覽器中輸入網址:localhost:8080可訪問呢

如果要直接將localhost定位到指定的前端工程。可以先將命令行定位到該工程的路徑下再運行http-server,是不是很簡單呢

192:~ xq$ cd /Applications/XAMPP/xamppfiles/htdocs/
192:htdocs xq$ http-server

這樣我在瀏覽器下直接輸入,localhost:8080/a.html. 就可以訪問htdocs下得a.html文件了

 

其他進階的:

http-server [path] [options]

path默認為當前文件夾,你可以在執行http-server之前先將終端定位到你要部署的服務器上的文件夾所在路徑。

options為各種選項。比如你可以http-server -p 8000 表示我要使用8000端口。

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')

-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 staring 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.

-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.


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM