Skip to content

Instantly share code, notes, and snippets.

@romaricpascal
Last active July 5, 2017 10:40
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save romaricpascal/8559206 to your computer and use it in GitHub Desktop.
Save romaricpascal/8559206 to your computer and use it in GitHub Desktop.
function startExpress() {
var express = require('express');
var app = express();
app.use(require('connect-livereload')());
app.use(express.static(__dirname));
app.listen(4000);
}
// Don't forget to install `gulp-liverload` beforehand:
// `npm install gulp-livereload --save-dev`
function notifyLivereload(event) {
gulp.src(event.path, {read: false})
.pipe(require('gulp-livereload')(lr));
}
var gulp = require('gulp');
// Let's make things more readable by
// encapsulating each part's setup
// in its own method
function startExpress() {
var express = require('express');
var app = express();
app.use(express.static(__dirname));
app.listen(4000);
}
gulp.task('default', function () {
startExpress();
});
var gulp = require('gulp');
var EXPRESS_PORT = 4000;
var EXPRESS_ROOT = __dirname;
var LIVERELOAD_PORT = 35729;
// Let's make things more readable by
// encapsulating each part's setup
// in its own method
function startExpress() {
var express = require('express');
var app = express();
app.use(require('connect-livereload')());
app.use(express.static(EXPRESS_ROOT));
app.listen(EXPRESS_PORT);
}
// We'll need a reference to the tinylr
// object to send notifications of file changes
// further down
var lr;
function startLivereload() {
lr = require('tiny-lr')();
lr.listen(LIVERELOAD_PORT);
}
// Notifies livereload of changes detected
// by `gulp.watch()`
function notifyLivereload(event) {
// `gulp.watch()` events provide an absolute path
// so we need to make it relative to the server root
var fileName = require('path').relative(EXPRESS_ROOT, event.path);
lr.changed({
body: {
files: [fileName]
}
});
}
// Default task that will be run
// when no parameter is provided
// to gulp
gulp.task('default', function () {
startExpress();
startLivereload();
gulp.watch('*.html', notifyLivereload);
});
var gulp = require('gulp');
// `gulp.task()` defines task that can be run calling `gulp xyz` from the command line
// The `default` task gets called when no task name is provided to Gulp
gulp.task('default', function () {
console.log('Gulp and running!')
});
// https://github.com/sindresorhus/jshint-stylish example
gulp.task('default', function () {
gulp.src(['file.js'])
.pipe(jshint('.jshintrc'))
.pipe(jshint.reporter('jshint-stylish'));
});
// ...
// We'll need a reference to the tinylr
// object to send notifications of file changes
var lr;
function startLivereload() {
lr = require('tiny-lr')();
lr.listen(35729);
}
// Default task that will be run
// when no parameter is provided
// to gulp
gulp.task('default', function () {
startExpress();
startLivereload();
});
@monokrome
Copy link

Typo here. It says gulp-liverload in gulp-livereload.snippet.js.

@foxx
Copy link

foxx commented Apr 30, 2015

Pretty useful, thanks!

@shortdiv
Copy link

I was having some issues with live reload, it kept returning undefined TypeError: undefined is not a function.

Changing line 9 to lr().listen(35729) seemed to solve that issue.
For more reference: [https://github.com/mklabs/tiny-lr]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment