nodejs搭建http-server


 

  很多時候我們都需要搭建一個簡單的服務器,部署在IIS,阿帕奇,或者用nodejs,網上很多關於nodejs搭建server的文章,但都是要創建server.js,很麻煩,

在這里我分享一個創建http-server的方法。去除繁瑣的創建server.js,我們可以這樣來實現全局的http-server的搭建,

我用到在nodejs里面提供了的http-server的插件 

  

全局安裝http-server插件 

npm install http-server -g

 

 這樣我們便實現了http-server 的全局安裝,

如果需要運行網站,就在我們的網站目錄下運行:

 

http-server [path] [options]

 

結果是:

其中path和option可以為空,默認為當前目錄

 

然后我們在瀏覽器中就可以訪問項目頁面了:比如:127.0.0.1:8080

 

option可用參數

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

-U or --utc Use UTC time format in log messages.

-P or --proxy Proxies all requests which can't be resolved locally to the given url. e.g.: -Phttp://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