gulp.js livereload with express server? gulp.js livereload with express server? express express

gulp.js livereload with express server?


I've added code that

  1. Detects changes in server files and reloads the server via nodemon

  2. Waits for a couple seconds after process reload in order to give the server time to run its initialization code.

  3. Triggers a change in a livereload server

note 1 : Your build should also include a livereload server and attach livereload scripts to html files before calling the 'serve' task

note 2: This is an asynchronous task that never ends, do not use it as a dependency of other tasks


gulp.task('serve', function (cb) {    nodemon({        script  : <server start file>,        watch   : <server files>        //...add nodeArgs: ['--debug=5858'] to debug         //..or nodeArgs: ['--debug-brk=5858'] to debug at server start    }).on('start', function () {        setTimeout(function () {            livereload.changed();        }, 2000); // wait for the server to finish loading before restarting the browsers    });});


Here's my solution:

//gulpfile.js:var gulp = require('gulp');var nodemon = require('gulp-nodemon');var server = require('gulp-express');var lr = require('tiny-lr')();gulp.task('default', function () {    nodemon({      script: 'server.js'    })    .on('restart', function () {        console.log('restarted!')    });    lr.listen(35729);    gulp.watch('server/**/*', function(event) {      var fileName = require('path').relative('3000', event.path);      lr.changed({        body: {          files: [fileName]        }      });    });});

You also need to include connect-livereload in your express server:

app.use(require('connect-livereload')());

Include it before bodyParser. I have read that this is not needed if you have the chrome live reload extension.


gulp-express is the right thing for you. It runs your express script as a server, with livereload!