1. 程式人生 > >live-server的使用

live-server的使用

原文地址:http://blog.csdn.net/shan1991fei/article/details/79007953

本地開發常常需要搭建臨時的服務,第一時間我們會想到用http-server

但現在流行修改檔案瀏覽器自動重新整理hot socketing(熱拔插),如live-reload

若想瀏覽器自動開啟專案,用opener

現在live-server實現了三個外掛的所有功能,並且很簡單就能啟動一個看起來很專業的本地服務

NPM全域性安裝

[javascript]  view plain  copy
  1. npm install -g live-server  
其它方式

[javascript]  view plain  copy
  1. git clone https://github.com/tapio/live-server  
  2. cd live-server  
  3. npm install # Local dependencies if
     you want to hack  
  4. npm install -g # Install globally  
使用方法:

1. 在專案根目錄使用命令

[javascript]  view plain  copy
  1. live-server  

引數:

[javascript]
  view plain  copy
  1. --port=NUMBER - select port to use, default: PORT env var or 8080  
  2. --host=ADDRESS - select host address to bind to, default: IP env var or 0.0.0.0 (“any address”)  
  3. --no-browser - suppress automatic web browser launching  
  4. --browser=BROWSER - specify browser to use instead of system default  
  5. --quiet | -q - suppress logging  
  6. --verbose | -V - more logging (logs all requests, shows all listening IPv4 interfaces, etc.)  
  7. --open=PATH - launch browser to PATH instead of server root  
  8. --watch=PATH - comma-separated string of paths to exclusively watch for changes (default: watch everything)  
  9. --ignore=PATH - comma-separated string of paths to ignore (anymatch-compatible definition)  
  10. --ignorePattern=RGXP - Regular expression of files to ignore (ie .*\.jade) (DEPRECATED in favor of --ignore)  
  11. --middleware=PATH - path to .js file exporting a middleware function to add; can be a name without path nor extension to reference bundled middlewares in middleware folder  
  12. --entry-file=PATH - serve this file (server root relative) in place of missing files (useful for single page apps)  
  13. --mount=ROUTE:PATH - serve the paths contents under the defined route (multiple definitions possible)  
  14. --spa - translate requests from /abc to /#/abc (handy for Single Page Apps)  
  15. --wait=MILLISECONDS - (default 100ms) wait for all changes, before reloading  
  16. --htpasswd=PATH - Enables http-auth expecting htpasswd file located at PATH  
  17. --cors - Enables CORS for any origin (reflects request origin, requests with credentials are supported)  
  18. --https=PATH - PATH to a HTTPS configuration module  
  19. --proxy=ROUTE:URL - proxy all requests for ROUTE to URL  
  20. --help | -h - display terse usage hint and exit  
  21. --version | -v - display version and exit  

2.  把它放在 package.json scripts 下的 server

[html]  view plain  copy
  1. "scripts": {  
  2.   "server": "live-server ./ --port=8081"  
  3. }  

執行

[javascript]  view plain  copy
  1. npm run server  

3. node

[javascript]  view plain  copy
  1. var liveServer = require("live-server");  
  2.    
  3. var params = {  
  4.     port: 8181, // Set the server port. Defaults to 8080.   
  5.     host: "0.0.0.0"// Set the address to bind to. Defaults to 0.0.0.0 or process.env.IP.   
  6.     root: "/public"// Set root directory that's being served. Defaults to cwd.   
  7.     open: false// When false, it won't load your browser by default.   
  8.     ignore: 'scss,my/templates'// comma-separated string for paths to ignore   
  9.     file: "index.html"// When set, serve this file for every 404 (useful for single-page applications)   
  10.     wait: 1000, // Waits for all changes, before reloading. Defaults to 0 sec.   
  11.     mount: [['/components''./node_modules']], // Mount a directory to a route.   
  12.     logLevel: 2, // 0 = errors only, 1 = some, 2 = lots   
  13.     middleware: [function(req, res, next) { next(); }] // Takes an array of Connect-compatible middleware that are injected into the server middleware stack   
  14. };  
  15. liveServer.start(params);