live-server安装
1.要想使用npm来安装live-server,我们首先要安装node.js和npm的依赖
安装nodejs和npm依赖参考博客连接:点击前往
2.准备工作完成后,使用(这种方式嘛,一般都比较慢)
npm install -g live-server (这种嘛,不抽风都还好)
cnpm install -g live-server (这种方式安装比较舒服)
live-server使用
1.命令行(cmd)进入项目的存放目录通过 live-server
启动默认是使用8080端口
live-server
2.如果不想使用默认的8080端口或者8080端口分配给别的服务器使用了(你是不是有别的猫了!!!)
live-server --port=8081
启动live-server时直接后面跟一个端口参数就行,简单就是每次都得这样有些繁琐
live-server --port=8081 (按自己心情给个端口号)
成功启动 live-server后,它后把目录下面的html文件展示在页面,点击你要打开的项目即可显示网页内容
注意:通过 crtl+c 可以退出live-server
3.live-server 的参数介绍
–port=NUMBER - select port to use, 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
不想每次启动都输入live-server,那就偷懒创建一个package.json来更好的摸鱼吧!!!
1.在项目根目录通过 npm init
来创建一个package.json以便我们配置
2.创建好package.json后使用文本编辑器打开它,在scripts里面添加 live-server启动时使用的端口
3.运行 npm run server
,来看看效果!
4.温馨小提示:如果浏览器不能实时自动刷新页面,可以试试用这个命令启动(亲测有效):live-server