一款npm工具,全局npm i -g live-server后,项目目录使用live-server命令行命令便可直接在浏览器中预览(默认找index.html,其他请自行带上文件名空格后跟在后面),并且自动全局监听实时更新。
命令行常用配置表
本地开发常常需要搭建临时的服务,第一时间我们会想到用http-server。
但现在流行修改文件浏览器自动刷新hot socketing(热拔插),如live-reload。
若想浏览器自动打开项目,用opener。
现在live-server实现了三个插件的所有功能,并且很简单就能启动一个看起来很专业的本地服务
NPM全局安装
npm install -g live-server
使用方法:
- 在项目根目录使用命令
npm init -y 初始化一个npm项目
- 之后项目中找到package.json,修改scripts部分
"scripts": {
"server": "live-server ./ --port=8081"
}
或者
"scripts": {
"server": "live-server 某个目录"
}
运行
npm run server
- node
var liveServer = require("live-server");
var params = {
port: 8181, // Set the server port. Defaults to 8080.
host: "0.0.0.0", // Set the address to bind to. Defaults to 0.0.0.0 or process.env.IP.
root: "/public", // Set root directory that's being served. Defaults to cwd.
open: false, // When false, it won't load your browser by default.
ignore: 'scss,my/templates', // comma-separated string for paths to ignore
file: "index.html", // When set, serve this file for every 404 (useful for single-page applications)
wait: 1000, // Waits for all changes, before reloading. Defaults to 0 sec.
mount: [['/components', './node_modules']], // Mount a directory to a route.
logLevel: 2, // 0 = errors only, 1 = some, 2 = lots
middleware: [function(req, res, next) { next(); }] // Takes an array of Connect-compatible middleware that are injected into the server middleware stack
};
liveServer.start(params);