UNPKG

4.62 kBMarkdownView Raw
1gulp-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
8gulp-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```
16npm install --save-dev gulp-connect
17```
18
19## Usage
20
21```js
22var gulp = require('gulp'),
23 connect = require('gulp-connect');
24
25gulp.task('connect', function() {
26 connect.server();
27});
28
29gulp.task('default', ['connect']);
30```
31
32#### LiveReload
33```js
34var gulp = require('gulp'),
35 connect = require('gulp-connect');
36
37gulp.task('connect', function() {
38 connect.server({
39 root: 'app',
40 livereload: true
41 });
42});
43
44gulp.task('html', function () {
45 gulp.src('./app/*.html')
46 .pipe(gulp.dest('./app'))
47 .pipe(connect.reload());
48});
49
50gulp.task('watch', function () {
51 gulp.watch(['./app/*.html'], ['html']);
52});
53
54gulp.task('default', ['connect', 'watch']);
55```
56
57
58#### Start and stop server
59
60```js
61gulp.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
75var gulp = require('gulp'),
76 stylus = require('gulp-stylus'),
77 connect = require('gulp-connect');
78
79gulp.task('connectDev', function () {
80 connect.server({
81 name: 'Dev App',
82 root: ['app', 'tmp'],
83 port: 8000,
84 livereload: true
85 });
86});
87
88gulp.task('connectDist', function () {
89 connect.server({
90 name: 'Dist App',
91 root: 'dist',
92 port: 8001,
93 livereload: true
94 });
95});
96
97gulp.task('html', function () {
98 gulp.src('./app/*.html')
99 .pipe(gulp.dest('./app'))
100 .pipe(connect.reload());
101});
102
103gulp.task('stylus', function () {
104 gulp.src('./app/stylus/*.styl')
105 .pipe(stylus())
106 .pipe(gulp.dest('./app/css'))
107 .pipe(connect.reload());
108});
109
110gulp.task('watch', function () {
111 gulp.watch(['./app/*.html'], ['html']);
112 gulp.watch(['./app/stylus/*.styl'], ['stylus']);
113});
114
115gulp.task('default', ['connectDist', 'connectDev', 'watch']);
116```
117
118#### http2 support
119
120If 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
126Type: `Array or String`
127Default: `Directory with gulpfile`
128
129The root path
130
131#### options.port
132
133Type: `Number`
134Default: `8080`
135
136The connect webserver port
137
138#### options.host
139
140Type: `String`
141Default: `localhost`
142
143#### options.name
144
145Type: `String`
146Default: `Server`
147
148The name that will be output when the server starts/stops.
149
150#### options.https
151
152Type: `Object`
153Default: `false`
154
155Can be any options documented at https://nodejs.org/api/https.html#https_https_createserver_options_requestlistener
156
157When https is just set to `true` (boolean), then internally some defaults will be used.
158
159#### options.livereload
160
161Type: `Object or Boolean`
162Default: `false`
163
164#### options.livereload.port
165
166Type: `Number`
167Default: `35729`
168
169Overrides the hostname of the script livereload injects in index.html
170
171#### options.livereload.hostname
172
173Type: `String`
174Default: 'undefined'
175
176#### options.fallback
177
178Type: `String`
179Default: `undefined`
180
181Fallback file (e.g. `index.html`)
182
183#### options.middleware
184
185Type: `Function`
186Default: `[]`
187
188#### options.debug
189
190Type: `Boolean`
191Default: `false`
192
193#### options.index
194
195Type: `Boolean or String of a new index pass or Array of new indexes in preferred order`
196Default: `true`
197
198```js
199gulp.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
213To contribute to this project, you must have CoffeeScript installed: `npm install -g coffee-script`.
214
215Then, 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)