本文是参考npm社区的gulp-connect。这个插件有什么作用,默默看图。基本上和gulp-webserver作用一样。

一、介绍:
这个gulp-connect启动服务器(并能时时同步)。
二、赞助
赞助者是JetBrains(好像是webstorm的编辑器开发者公司)。
三、安装
npm install --save-dev gulp-connect
四、说明
var gulp = require('gulp'),
connect = require('gulp-connect');
gulp.task('connect', function() {
connect.server();
});
gulp.task('default', ['connect']);
LiveReload
var gulp = require('gulp'),
connect = require('gulp-connect');
gulp.task('connect', function() {
connect.server({
root: 'app',
livereload: true
});
});
gulp.task('html', function () {
gulp.src('./app/*.html')
.pipe(connect.reload());
});
gulp.task('watch', function () {
gulp.watch(['./app/*.html'], ['html']);
});
gulp.task('default', ['connect', 'watch']);
启动和关闭服务器
gulp.task('jenkins-tests', function() {
connect.server({
port: 8888
});
// run some headless tests with phantomjs
// when process exits:
connect.serverClose();
});
启动多个服务器
var gulp = require('gulp'),
stylus = require('gulp-stylus'),
connect = require('gulp-connect');
gulp.task('connectDev', function () {
connect.server({
name: 'Dev App',
root: ['app', 'tmp'],
port: 8000,
livereload: true
});
});
gulp.task('connectDist', function () {
connect.server({
name: 'Dist App',
root: 'dist',
port: 8001,
livereload: true
});
});
gulp.task('html', function () {
gulp.src('./app/*.html')
.pipe(connect.reload());
});
gulp.task('stylus', function () {
gulp.src('./app/stylus/*.styl')
.pipe(stylus())
.pipe(gulp.dest('./app/css'))
.pipe(connect.reload());
});
gulp.task('watch', function () {
gulp.watch(['./app/*.html'], ['html']);
gulp.watch(['./app/stylus/*.styl'], ['stylus']);
});
gulp.task('default', ['connectDist', 'connectDev', 'watch']);
http2的支持情况
如果http2的安装包已经安装,你用https联动到gulp connect插件,然后http2将成为首选。
五、API
api参数比较多,这里说一些常用的参数,其他参数和localhost的属性很像,不再累赘(点击获取详情)
1、option.root
str array 启动服务器的目录
2、option.port
服务器的端口,默认是3000
3、options.livereload
boolean, 是否开启时时同步,默认是false,通常是开启的
提供一个简单的demo代码地址
六、结合open使用
(详情还是上npm社区),可以在在局域网共享,用法很简单
var open = require('open');
/*
* open(param1,param2)
* param1 str url
* param2 str browserName
*/
open('http://192.168.0.200:1234','chrome');
