# gulp-server-livereload

> Gulp plugin to run a local webserver with livereload enabled via socket.io. Also comes with standalone command-line interface.

Latest version **1.9.2** (published 2016-11-04) · MIT license · 0 weekly downloads

## Install

```sh
npm install gulp-server-livereload
pnpm add gulp-server-livereload
yarn add gulp-server-livereload
bun add gulp-server-livereload
```

Provides the command `livereload`.

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.9.2 |
| Published | 2016-11-04 |
| First published | 2014-08-16 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 16 |
| Known vulnerabilities | 0 (+7 in 5 direct dependencies) |
| Install scripts | no |
| GitHub stars | 90 |
| Author | Ramesh Nair |
| Maintainers | hiddentao |
| Keywords | gulpplugin, webserver, connect, livereload, socket, websocket |

## Links

- npm: https://www.npmjs.com/package/gulp-server-livereload
- Repository: https://github.com/hiddentao/gulp-server-livereload
- Issues: https://github.com/hiddentao/gulp-server-livereload/issues
- npm.io page: https://npm.io/package/gulp-server-livereload

## Dependencies (16)

- [open](https://npm.io/package/open.md) ~0.0.5
- [glogg](https://npm.io/package/glogg.md) ^1.0.0
- [lodash](https://npm.io/package/lodash.md) ^4.0.0
- [connect](https://npm.io/package/connect.md) ~3.1.1
- [gulplog](https://npm.io/package/gulplog.md) ^1.0.0
- [through2](https://npm.io/package/through2.md) ~0.5.1
- [vinyl-fs](https://npm.io/package/vinyl-fs.md) ^1.0.0
- [commander](https://npm.io/package/commander.md) ^2.8.1
- [gulp-util](https://npm.io/package/gulp-util.md) ^3.0.7
- [socket.io](https://npm.io/package/socket.io.md) ^1.4.4
- [node-watch](https://npm.io/package/node-watch.md) ^0.3.4
- [node.extend](https://npm.io/package/node.extend.md) ~1.0.10
- [serve-index](https://npm.io/package/serve-index.md) ~1.1.4
- [serve-static](https://npm.io/package/serve-static.md) ~1.5.2
- [connect-inject](https://npm.io/package/connect-inject.md) ~0.3.2
- [proxy-middleware](https://npm.io/package/proxy-middleware.md) ~0.15.0

## Alternatives

- [@opentelemetry/exporter-zipkin](https://npm.io/package/@opentelemetry/exporter-zipkin.md) — 14.8M weekly downloads
- [pusher-js](https://npm.io/package/pusher-js.md) — 2.0M weekly downloads
- [browserify](https://npm.io/package/browserify.md) — 1.7M weekly downloads
- [sqs-consumer](https://npm.io/package/sqs-consumer.md) — 1.7M weekly downloads
- [@sanity/eventsource](https://npm.io/package/@sanity/eventsource.md) — 930.8K weekly downloads

## Recent versions

- 1.9.2 (latest) — 2016-11-04
- 1.9.1 — 2016-10-28
- 1.9.0 — 2016-10-14
- 1.8.4 — 2016-09-01
- 1.8.3 — 2016-09-01
- 1.8.2 — 2016-07-21
- 1.8.1 — 2016-06-12
- 1.8.0 — 2016-05-30
- 1.7.4 — 2016-04-13
- 1.7.3 — 2016-03-14
- 1.7.2 — 2016-03-14
- 1.7.1 — 2016-02-29
- 1.7.0 — 2016-02-01
- 1.6.2 — 2016-01-20
- 1.6.1 — 2016-01-20
- … 18 more at https://npm.io/package/gulp-server-livereload/versions

## README

gulp-server-livereload [![Build Status](http://img.shields.io/travis/hiddentao/gulp-server-livereload.svg?style=flat)](https://travis-ci.org/hiddentao/gulp-server-livereload)
==============

> Gulp plugin to run a local webserver with live reload using socket.io

Serve a folder over HTTP and watch it for changes, telling the browser to
reload itself when a change happens.

* Uses [socket.io](http://socket.io) - livereload mechanism works even
if your browser does not support WebSockets (PhoneGap developers rejoice!).

* `window.console` capture - it can capture `console` output from the
client-side and transmit it to the back-end for display. This is useful for
when testing from Phonegap, etc.

* Supports [CSS injection](#livereload-behaviour) (no need to reload the whole page if just your CSS
has changed).

* Proxy mode - proxy requests arriving at certain URLs to other servers.

* Comes with a command-line runnable.



## Installation

```sh
$ npm install --save-dev gulp-server-livereload
```

## Usage

The folder supplied to `gulp.src()` will be the root folder from which files will be served.

```js
var gulp = require('gulp');
var server = require('gulp-server-livereload');

gulp.task('webserver', function() {
  gulp.src('app')
    .pipe(server({
      livereload: true,
      directoryListing: true,
      open: true
    }));
});
```

If you run `gulp webserver` your browser should automatically open up to `http://localhost:8000` and show a directory listing of the `app` folder.

### Command-line

Install the package globally:

```bash
$ npm install -g gulp-server-livereload
```

Then you can run the `livereload` command to serve files out of the current folder.
Here are the available options:

```bash
$ livereload help

  Usage: livereload [options]

  Options:

    -h, --help        output usage information
    -V, --version     output the version number
    -n, --no-browser  do not open the localhost server in a browser
    -l, --log [type]  log level (default: info)
    -p, --port <n>    the port to run on
```


## Options

_Note: not all of these options are currently available via the CLI executable_

Key | Type | Default | Description
--- | --- | --- | ---
`host` | String | `localhost` | hostname of the webserver
`port` | Number | `8000` | port of the webserver
`livereload` | Boolean/Object | `false` | whether to use livereload. For advanced options, provide an object with `enable` set to `true`.
`livereload.port` | Number | `35729` | port for livereload server to listen on.
`livereload.markupHost` | String | `null` | the hostname to use for the livereload server in the injected SCRIPT tag. Default is to calculate it dynamically in the browser.
`livereload.filter` | Function | - | function to filter out files to watch (default filters out `node_modules`).
`livereload.clientConsole` | Boolean | `false` | whether to capture `window.console` output from the client and send it to the back-end for display.
`directoryListing` | Boolean/Object | `false` | whether to display a directory listing. For advanced options, provide an object. You can use the `path` property to set a custom path or the `options` property to set custom [serve-index](https://github.com/expressjs/serve-index) options.
`defaultFile` | String | `index.html` | default file to show when root URL is requested. If `directoryListing` is enabled then this gets disabled.
`fallback` | String | `undefined` | file to fall back to (relative to webserver root) when requested resource not found. Useful when building single-page apps with non-has URLs.
`fallbackLogic` | Function | `see index.js` | Middleware function responsible for writing the fallback file to output, or anything else you might want to do instead.
`open` | Boolean/Object | `false` | open the localhost server in the browser
`https` | Boolean/Object | `false` | whether to use https or not. By default, `gulp-server-livereload` provides you with a development certificate but you remain free to specify a path for your key and certificate by providing an object like this one: `{key: 'path/to/key.pem', cert: 'path/to/cert.pem'}`.
`log` | String | `info` | If set to `debug` you will see all requests logged to the console.
`clientLog` | String | `debug` | Chooses the level of logging to report from the client in the console if `livereload.clientConsole` is enabled. Available choices: `debug`, `info`, `warn`, `error`.
`proxies` | Array | `[]`| a list of proxy objects.  Each proxy object can be specified by `{source: '/abc', target: 'http://localhost:8080/abc', options: {headers: {'ABC_HEADER': 'abc'}}}`.


## Livereload behaviour

By default when a file changes the livereload script in the browser does the
following:

1. Checks to see whether the changed file is a CSS file
2. If it is a CSS file then it reloads the changed CSS files in the browser
3. Otherwise it reloads the whole page

To override the default behaviour define the following method in Javascript:

```js
/**
 * This method gets called by the livereload script when the server notifies it
 * that something has changed.
 *
 * @param  {Object} file File which changed.
 */
window._onLiveReloadFileChanged = function(file) {
  // do whatever you want here, e.g. location.reload();
}
```

The `file` parameter has the following structure:

```js
{
  "path": ...full path to file which changed...
  "name": ...file name (without path)...
  "ext": ...file extension name...
}
```

## FAQ

#### Why can't I reach the server from the network?

Set `0.0.0.0` as the `host` option.

#### How can I set main.html to automatically load when I visit the URL?

Set the `defaultFile` to `main.html`:

```js
gulp.task('webserver', function() {
  gulp.src('app')
    .pipe(server({
      defaultFile: 'main.html'
    }));
});
```

#### How can I use livereload if my HTML is already being served up by a node.js/other app?

You'll have to add some Javascript to dynamically load in the browser-side scripts.
For example, if the `gulp-server-livereload` livereload port is set to 34322 then you would add:

```js
(function() {
  var lrHost = location.protocol + '//' + location.hostname + ':34322';
  var s = document.createElement('script');
  s.async = true;
  s.setAttribute('src', lrHost + '/livereload.js');
  document.body.appendChild(s);
})();
```

To enable console logging capture add the following query paramter:

```js
s.setAttribute('src', lrHost + '/livereload.js?extra=capture-console');
```


#### How can I pass a custom filter to livereload?

In the `livereload` object, set the `enable` to `true` and provide filter function in `filter`:

```js
gulp.task('webserver', function() {
  gulp.src('app')
    .pipe(server({
      livereload: {
        enable: true,
        filter: function(filePath, cb) {
          cb( !(/node_modules/.test(filePath)) );
        }
      }
    }));
});
```

#### How can I use non-hash URLs for my single page app (i.e. HTML5 mode) with this plugin?

When you're building a single-page app with non-hash URLs (html5 mode) then you
want the server to always serve up the same file for every URL. This is where
the `fallback` option comes into play:

```js
gulp.task('webserver', function() {
  gulp.src('app')
    .pipe(server({
      fallback: 'index.html'
    }));
});
```

You can control exactly how the fallback mode works using the `fallbackLogic`
parameter. For example, if you wanted to handle PNG files separately and not
have the `fallback` get returned for such requests:

```js
gulp.task('webserver', function() {
  gulp.src('app')
    .pipe(server({
      fallback: 'index.html',
      fallbackLogic: function(req, res, fallbackFile) {
        if (req.url.match(/\.png$/i)) {
          res.statusCode = 404;
          res.end();
        } else {
          // default fallback config
          fs.createReadStream(fallbackFile).pipe(res);
        }
      },
    }));
});
```


### How can I use this with CSS preprocessors such as LESS or SASS?

If you use a CSS preprocessor in gulp, you'll need to run its gulp task (typically with `gulp-watch`) together with the server, so that  LESS/SASS files are compiled as you save.

You'll also want to configure livereload to ignore changes to the source files, and instead let it only handle changes to the compiled CSS (which will be refreshed inline).

```js
var watch = require('gulp-watch');

gulp.task('watch', function () {
  watch('./styles/*.less', batch(function (events, done) {
    gulp.start('default', done);
  }));
});

gulp.task('webserver', ['watch'], function () {
  gulp.src('.')
    .pipe(server({
      livereload: {
        enable: true,
        filter: function (filename, cb) {
          cb(!/\.(sa|le)ss$|node_modules/.test(filename);
        }
      },
      directoryListing: true,
      open: true
    }));
});
```

_Note: the livereload server automatically handles generated sourcemap files properly so don't worry about them_.

## License

MIT - see LICENSE.md

---
_Source: https://npm.io/package/gulp-server-livereload · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
