Gulp plugin to run a webserver (with LiveReload)

Install

npm can help us to install the plugin.

PS C:\study\gulp> npm install --save-dev gulp-connect
gulp-connect@2.2. node_modules\gulp-connect
├── connect-livereload@0.3.
├── event-stream@3.1. (duplexer@0.1., from@0.1., stream-combiner@0.0., pause-stream@0.0., map-stream@0.1., split@0.2., through@2.3.)
├── tiny-lr@0.0. (debug@0.8., qs@0.5., faye-websocket@0.4., noptify@0.0.)
├── gulp-util@2.2. (lodash._reinterpolate@2.4., minimist@0.2., vinyl@0.2., through2@0.5., chalk@0.5., multipipe@0.1., dateformat@1.0., lodash.template@2.4.)
└── connect@2.17. (parseurl@1.0., response-time@1.0., cookie@0.1., cookie-signature@1.0., fresh@0.2., debug@0.8., connect-timeout@1.1., vhost@1.0., qs@0.6., bytes@1.0., basic-auth-connect@1.0., on-headers@0.0., serve-favicon@2.0., errorhandler@1.0., morgan@1.1., cookie-parser@1.1., pause@0.0., type-is@1.2., method-override@1.0., compression@1.0., body-parser@1.2., express-session@1.2., csurf@1.2., serve-index@1.0., serve-static@1.1., multiparty@.
2.0)

you can saw the connect-livereload already contained.

Usage

we get a connect object, it help us to serve static web server. default, the web server root is the location of gulpfile.js.

create a js file, named to gulpfile.js, it is the specification name for gulp.

var  gulp = require('gulp'),
connect = require('gulp-connect'); gulp.task('connect', function() {
//
connect.server();
}); gulp.task('default', ['connect']);

open browser that your favorited, locate to http://localhost:8080/

default, it support ditionary browser, so you should saw the dictionary.

LiveReload

you should use watch feature with livereload, so you will create watch task for it.

in watch task, when file changed, watch task trigger specification task, in below, it is html task.

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']);

in html task, we reload  target files.

Start and stop server

connect.server start the web server, connect.serverClose to close a web server.

gulp.task('jenkins-tests', function() {
connect.server({
port: 8888
});
// run some headless tests with phantomjs
// when process exits:
connect.serverClose();
});

Multiple server

var gulp = require('gulp'),
stylus = require('gulp-stylus'),
connect = require('gulp-connect'); gulp.task('connectDev', function () {
connect.server({
root: ['app', 'tmp'],
port: 8000,
livereload: true
});
}); gulp.task('connectDist', function () {
connect.server({
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']);

API

options.root

Type: Array or String Default: Directory with gulpfile

The root path

options.port

Type: Number Default: 8080

The connect webserver port

options.host

Type: String Default: localhost

options.https

Type: Boolean Default: false

options.livereload

Type: Object or Boolean Default: false

options.livereload.port

Type: Number Default: 35729

options.fallback

Type: String Default: undefined

Fallback file (e.g. index.html)

options.middleware

Type: Function Default: []

gulp.task('connect', function() {
connect.server({
root: "app",
middleware: function(connect, opt) {
return [
// ...
]
}
});
});

Contributors

AveVlad and schickling

gulp - connect的更多相关文章

  1. gulp connect.static is not a function

    npm install --save serve-static var serveStatic = require('serve-static');

  2. 前端打包构建工具gulp快速入门

    因为之前一直有人给我推荐gulp,说他这里好哪里好的.实际上对我来说够用就行.grunt熟悉以后实际上他的配置也不难,说到效率的话确实是个问题,尤其项目大了以后,目前位置遇到的项目都还可以忍受.不过不 ...

  3. gulp启动一个小型web服务器配置&browserify(require)

    var gulp = require('gulp'), connect = require('gulp-connect'), // 运行live reload服务器 browserify = requ ...

  4. Gulp实现web服务器

    Gulp实现web服务器 阅读目录 一:gulp实现web服务器配置: 二:添加实时刷新(livereload)支持 回到顶部 一:gulp实现web服务器配置: 对于前端开发而言,需要在本地搭建一个 ...

  5. 使用gulp插件来自动刷新页面。

    http://itakeo.com/blog/2016/05/19/gulpreload/?none=123 使用gulp插件来自动刷新页面.再也不用修改一次,按一下F5了. 首选通过npm inst ...

  6. gulp api

    gulp api 简介 gulp是前端开发过程中对代码进行构建的工具,是自动化项目的构建利器:它不仅能对网站资源进行优化,而且在开发过程中很多重复的任务能够使用正确的工具自动完成 gulp是基于Nod ...

  7. Gulp实现服务器

    Gulp实现web服务器 Gulp实现web服务器 阅读目录 一:gulp实现web服务器配置: 二:添加实时刷新(livereload)支持 回到顶部 一:gulp实现web服务器配置: 对于前端开 ...

  8. Angular企业级开发(6)-使用Gulp构建和打包前端项目

    1.gulp介绍 基于流的前端自动化构建工具,利用gulp可以提高前端开发效率,特别是在前后端分离的项目中.使用gulp能完成以下任务: 压缩html.css和js 编译less或sass等 压缩图片 ...

  9. gulp点滴

    var gulp = require('gulp'), connect = require('gulp-connect'), browserify = require('gulp-browserify ...

随机推荐

  1. 一些C#预处理器指令

    像C语言一样,C#有一些预处理器指令的命令.例如,#if#end if,#define等,所谓这些命令是指不会转化为可执行代码中的一些命令,只是在编译的过程中起作用.下面简要介绍一下:1 .#defi ...

  2. LintCode "Coins in a Line"

    Recursion + Memorized Search(DP). And apparently, the code below can be iterative with only 3 vars - ...

  3. USB 2.0 Spec 微缩版

    4.1.1 Bus Topology 最大层数为7,第7层只能是Function不能是Hub,非根Hub最大5级. 5.3 USB Communication Flow Host Controller ...

  4. label标签的用法

    label 标签for属性 <h1>显式指定通过for(for的值就是对应radio的id的值)</h1> <form> <label for="m ...

  5. php测试程序运行时间和占用内存情况

    php测试程序运行时间和占用内存情况: $HeaderTime = microtime(true);//参数true表示返回浮点数值 /** *CODE */ printf(" total ...

  6. [Android实例] Scroll原理-附ScrollView源码分析

    想象一下你拿着放大镜贴很近的看一副巨大的清明上河图, 那放大镜里可以看到的内容是很有限的, 而随着放大镜的上下左右移动,就可以看到不同的内容了 android中手机屏幕就相当于这个放大镜, 而看到的内 ...

  7. PLSQL_Database Link的基本概念和用法(概念)

    2014-06-08 Created By BaoXinjian

  8. centos nginx环境下删除CI框架Index.php入口遇到404问题

    今天在网上百度看了很多文章,想要去掉index.php入口文件有好多方法,自己也照着在网站到根目录下新建了一个.htaccess文件,内容如下: RewriteEngine On RewriteCon ...

  9. Hololens开发笔记之Gesture手势识别(基本介绍)

    手势识别是HoloLens交互的重要输入方法之一.HoloLens提供了底层API和高层API,可以满足不同的手势定制需求.底层API能够获取手的位置和速度信息,高层API则借助手势识别器来识别预设的 ...

  10. JavaScript: bind apply call

    var foo = function(age,sex){ console.log(this.name,age,sex); }; //call将改变函数运行的context foo.call({name ...