http-server 是一個簡單的零配置命令行HTTP服務器, 基於 nodeJs.php
若是你不想重複的寫 nodeJs 的 web-server.js, 則能夠使用這個.html
安裝 (全局安裝加 -g) : node
npm install http-server
若是警告:npm WARN enoent ENOENT: no such file or directory, open 'D:\myserver\package.json'web
解決:這是項目目錄下沒有package.json這個文件。能夠使用npm init -f命令生成一下,至於生成的package.json中缺乏的字段你能夠參照其餘的模塊的package.json文件填進去。至於package.json中的每一個字段的值能夠爲「」。至於依賴項字段,以安裝模塊的時候使用-save參數就會自動寫到文件中。下面是我測試用的packa.json文件npm
{json
"name": "test",瀏覽器
"version": "1.0.0",服務器
"description": "23411",cors
"main": "index.js",ide
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"repository": {
"type": "",
"url": ""
}
}
粘貼上:
再"scripts": {
"start": "http-server -a 0.0.0.0 -p 8000",
}
Windows 下使用:
在站點目錄下開啓命令行輸入
http-server
先創建個文件夾如D:\myserver 而後cd
在這個文件夾下啓命令行http-server
這時若是報錯:不是內部或外部命令,也不是可運行的程序參考資料
解決(來源http://jingyan.baidu.com/article/4f7d5712ce8c1f1a201927fa.html):把計算機屬性的環境變量path F:\AppServ\Apache24\bin;F:\AppServ\php5;F:\AppServ\MySQL\bin;C:\Users\Administrator\AppData\Roaming\npm;C:\Program Files\nodejs\;C:\Program Files\nodejs\node_global;C:\Windows\System32;D:\myserver
修改環境變量後不須要重啓電腦,只須要從新打開cmd就行
訪問: http://localhost:8080 or http://127.0.0.1:8080 就能夠訪問到index.html文件了
使用於package.json
"scripts": { "start": "http-server -a 0.0.0.0 -p 8000", }
參數 :
-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 打印以上列表並退出