Total Pageviews

Wednesday, 12 April 2017

又一个静态服务器程序live-server

A simple development http server with live reload capability.

Having the page reload automatically after changes to files can accelerate development.

You don't need to install any browser plugins or manually add code snippets to your pages for the reload functionality to work, see "How it works" section below for more information. If you don't want/need the live reload, you should probably use something even simpler, like the following Python-based one-liner:
python -m SimpleHTTPServer

Installation

You need node.js and npm. You should probably install this globally.
Npm way
npm install -g live-server
Manual way
git clone https://github.com/tapio/live-server
cd live-server
npm install # Local dependencies if you want to hack
npm install -g # Install globally

Usage from command line

Issue the command live-server in your project's directory. Alternatively you can add the path to serve as a command line parameter.
This will automatically launch the default browser. When you make a change to any file, the browser will reload the page - unless it was a CSS file in which case the changes are applied without a reload.
Command line parameters:
  • --port=NUMBER - select port to bind, default: PORT env var or 8080
  • --host=ADDRESS - select host address to bind to, default: IP env var or 0.0.0.0 ("any address")
  • --no-browser - suppress automatic web browser launching
  • --browser=BROWSER - specify browser to use instead of system default
  • --quiet | -q - suppress logging
  • --verbose | -V - more logging (logs all requests, shows all listening IPv4 interfaces, etc.)
  • --open=PATH - launch browser to PATH instead of server root
  • --watch=PATH - comma-separated string of paths to exclusively watch for changes (default: watch everything)
  • --ignore=PATH - comma-separated string of paths to ignore (anymatch-compatible definition)
  • --ignorePattern=RGXP - Regular expression of files to ignore (ie .*\.jade) (DEPRECATED in favor of --ignore)
  • --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
  • --entry-file=PATH - serve this file (server root relative) in place of missing files (useful for single page apps)
  • --mount=ROUTE:PATH - serve the paths contents under the defined route (multiple definitions possible)
  • --spa - translate requests from /abc to /#/abc (handy for Single Page Apps)
  • --wait=MILLISECONDS - (default 100ms) wait for all changes, before reloading
  • --htpasswd=PATH - Enables http-auth expecting htpasswd file located at PATH
  • --cors - Enables CORS for any origin (reflects request origin, requests with credentials are supported)
  • --https=PATH - PATH to a HTTPS configuration module
  • --proxy=ROUTE:URL - proxy all requests for ROUTE to URL
  • --help | -h - display terse usage hint and exit
  • --version | -v - display version and exit
Default options:
If a file ~/.live-server.json exists it will be loaded and used as default options for live-server on the command line. See "Usage from node" for option names.

How it works

The server is a simple node app that serves the working directory and its subdirectories. It also watches the files for changes and when that happens, it sends a message through a web socket connection to the browser instructing it to reload. In order for the client side to support this, the server injects a small piece of JavaScript code to each requested html file. This script establishes the web socket connection and listens to the reload requests. CSS files can be refreshed without a full page reload by finding the referenced stylesheets from the DOM and tricking the browser to fetch and parse them again.


from  https://github.com/tapio/live-server









http://gcv.bright.biz.st:21546/posts/traveler-song/

No comments:

Post a Comment