1 | gulp-connect [![Build Status](http://img.shields.io/travis/AveVlad/gulp-connect.svg?style=flat-square)](https://travis-ci.org/AveVlad/gulp-connect) [![](http://img.shields.io/npm/dm/gulp-connect.svg?style=flat-square)](https://www.npmjs.org/package/gulp-connect) [![](http://img.shields.io/npm/v/gulp-connect.svg?style=flat-square)](https://www.npmjs.org/package/gulp-connect) [![Join the chat at https://gitter.im/AveVlad/gulp-connect](https://badges.gitter.im/AveVlad/gulp-connect.svg)](https://gitter.im/AveVlad/gulp-connect?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
|
2 | ==============
|
3 |
|
4 | > Gulp plugin to run a webserver (with LiveReload)
|
5 |
|
6 | ## Sponsors
|
7 |
|
8 | gulp-connect is sponsored by [JetBrains](https://www.jetbrains.com/)!
|
9 |
|
10 | <a href="https://www.jetbrains.com/" taget="_blank"><img src="jetbrains.png" height="150" width="150"></a>
|
11 |
|
12 |
|
13 | ## Install
|
14 |
|
15 | ```
|
16 | npm install --save-dev gulp-connect
|
17 | ```
|
18 |
|
19 | ## Usage
|
20 |
|
21 | ```js
|
22 | var gulp = require('gulp'),
|
23 | connect = require('gulp-connect');
|
24 |
|
25 | gulp.task('connect', function() {
|
26 | connect.server();
|
27 | });
|
28 |
|
29 | gulp.task('default', ['connect']);
|
30 | ```
|
31 |
|
32 | #### LiveReload
|
33 | ```js
|
34 | var gulp = require('gulp'),
|
35 | connect = require('gulp-connect');
|
36 |
|
37 | gulp.task('connect', function() {
|
38 | connect.server({
|
39 | root: 'app',
|
40 | livereload: true
|
41 | });
|
42 | });
|
43 |
|
44 | gulp.task('html', function () {
|
45 | gulp.src('./app/*.html')
|
46 | .pipe(gulp.dest('./app'))
|
47 | .pipe(connect.reload());
|
48 | });
|
49 |
|
50 | gulp.task('watch', function () {
|
51 | gulp.watch(['./app/*.html'], ['html']);
|
52 | });
|
53 |
|
54 | gulp.task('default', ['connect', 'watch']);
|
55 | ```
|
56 |
|
57 |
|
58 | #### Start and stop server
|
59 |
|
60 | ```js
|
61 | gulp.task('jenkins-tests', function() {
|
62 | connect.server({
|
63 | port: 8888
|
64 | });
|
65 | // run some headless tests with phantomjs
|
66 | // when process exits:
|
67 | connect.serverClose();
|
68 | });
|
69 | ```
|
70 |
|
71 |
|
72 | #### Multiple server
|
73 |
|
74 | ```js
|
75 | var gulp = require('gulp'),
|
76 | stylus = require('gulp-stylus'),
|
77 | connect = require('gulp-connect');
|
78 |
|
79 | gulp.task('connectDev', function () {
|
80 | connect.server({
|
81 | name: 'Dev App',
|
82 | root: ['app', 'tmp'],
|
83 | port: 8000,
|
84 | livereload: true
|
85 | });
|
86 | });
|
87 |
|
88 | gulp.task('connectDist', function () {
|
89 | connect.server({
|
90 | name: 'Dist App',
|
91 | root: 'dist',
|
92 | port: 8001,
|
93 | livereload: true
|
94 | });
|
95 | });
|
96 |
|
97 | gulp.task('html', function () {
|
98 | gulp.src('./app/*.html')
|
99 | .pipe(gulp.dest('./app'))
|
100 | .pipe(connect.reload());
|
101 | });
|
102 |
|
103 | gulp.task('stylus', function () {
|
104 | gulp.src('./app/stylus/*.styl')
|
105 | .pipe(stylus())
|
106 | .pipe(gulp.dest('./app/css'))
|
107 | .pipe(connect.reload());
|
108 | });
|
109 |
|
110 | gulp.task('watch', function () {
|
111 | gulp.watch(['./app/*.html'], ['html']);
|
112 | gulp.watch(['./app/stylus/*.styl'], ['stylus']);
|
113 | });
|
114 |
|
115 | gulp.task('default', ['connectDist', 'connectDev', 'watch']);
|
116 | ```
|
117 |
|
118 | #### http2 support
|
119 |
|
120 | If the [http2](https://www.npmjs.com/package/http2) package is installed and you use an https connection to gulp connect then http 2 will be used in preference to http 1.
|
121 |
|
122 | ## API
|
123 |
|
124 | #### options.root
|
125 |
|
126 | Type: `Array or String`
|
127 | Default: `Directory with gulpfile`
|
128 |
|
129 | The root path
|
130 |
|
131 | #### options.port
|
132 |
|
133 | Type: `Number`
|
134 | Default: `8080`
|
135 |
|
136 | The connect webserver port
|
137 |
|
138 | #### options.host
|
139 |
|
140 | Type: `String`
|
141 | Default: `localhost`
|
142 |
|
143 | #### options.name
|
144 |
|
145 | Type: `String`
|
146 | Default: `Server`
|
147 |
|
148 | The name that will be output when the server starts/stops.
|
149 |
|
150 | #### options.https
|
151 |
|
152 | Type: `Object`
|
153 | Default: `false`
|
154 |
|
155 | Can be any options documented at https://nodejs.org/api/https.html#https_https_createserver_options_requestlistener
|
156 |
|
157 | When https is just set to `true` (boolean), then internally some defaults will be used.
|
158 |
|
159 | #### options.livereload
|
160 |
|
161 | Type: `Object or Boolean`
|
162 | Default: `false`
|
163 |
|
164 | #### options.livereload.port
|
165 |
|
166 | Type: `Number`
|
167 | Default: `35729`
|
168 |
|
169 | Overrides the hostname of the script livereload injects in index.html
|
170 |
|
171 | #### options.livereload.hostname
|
172 |
|
173 | Type: `String`
|
174 | Default: 'undefined'
|
175 |
|
176 | #### options.fallback
|
177 |
|
178 | Type: `String`
|
179 | Default: `undefined`
|
180 |
|
181 | Fallback file (e.g. `index.html`)
|
182 |
|
183 | #### options.middleware
|
184 |
|
185 | Type: `Function`
|
186 | Default: `[]`
|
187 |
|
188 | #### options.debug
|
189 |
|
190 | Type: `Boolean`
|
191 | Default: `false`
|
192 |
|
193 | #### options.index
|
194 |
|
195 | Type: `Boolean or String of a new index pass or Array of new indexes in preferred order`
|
196 | Default: `true`
|
197 |
|
198 | ```js
|
199 | gulp.task('connect', function() {
|
200 | connect.server({
|
201 | root: "app",
|
202 | middleware: function(connect, opt) {
|
203 | return [
|
204 | // ...
|
205 | ]
|
206 | }
|
207 | });
|
208 | });
|
209 | ```
|
210 |
|
211 | ## Contributing
|
212 |
|
213 | To contribute to this project, you must have CoffeeScript installed: `npm install -g coffee-script`.
|
214 |
|
215 | Then, to build the `index.js` file, run `coffee -o . -bc src/`. Run `npm test` to run the tests.
|
216 |
|
217 | ## Contributors
|
218 |
|
219 | * [AveVlad](https://github.com/AveVlad)
|
220 | * [schickling](https://github.com/schickling)
|
221 | * [justinmchase](https://github.com/justinmchase)
|