gulp.js livereload with express server?

Posted on

gulp.js livereload with express server?

I am trying to setup my gulpfile.js to use livereload on an express server without much luck. I see examples out there but they seem to be related to a static file server.–cms-20903

So I have an app.js file which does the standard express server with jade files, etc. What I want to do is get it to work with livereload from a gulp.js boot.

app.set('port', process.env.PORT || 3000);
    var server = app.listen(app.get('port'), function() {
    debug('Express server listening on port ' + server.address().port);

There are lots of plugins like gulp-livereload, connect-livereload, gulp-connect, gulp-watch so how can I get this wired up?

Solution :

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) {
        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 () {
        }, 2000); // wait for the server to finish loading before restarting the browsers

Here’s my solution:

var gulp = require('gulp');
var nodemon = require('gulp-nodemon');
var server = require('gulp-express');
var lr = require('tiny-lr')();

gulp.task('default', function () {
      script: 'server.js'
    .on('restart', function () {

    lr.listen(35729);'server/**/*', function(event) {
      var fileName = require('path').relative('3000', event.path);
        body: {
          files: [fileName]

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


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!

I had the same issue and was not able to find anything related. My solution ends up with the following piece of code in Gulpfile:

var fork = require('child_process').fork;
var tinyLr = require('tiny-lr');
var async = require('async');
var plugins = require('gulp-load-plugins')({ lazy: false });

var lr = null;
var lrPort = 35729;
var buildDir = 'build';
var serverAppFile = path.join(__dirname, 'build/server/app.js');

// This guy starts and stops nodejs process which runs our Express app
var app = {
    start: function(callback) {
        var instance = app.instance = fork(serverAppFile, {
            silent: true
        app.dataListener = function(data) {
            var message = '' + data;
            // Assume that server is started when it prints the following to stdout
            if (message.indexOf('Express server listening on port') === 0) {
        instance.stdout.on('data', app.dataListener);

    stop: function(callback) {
        app.instance.stdout.removeListener('data', app.dataListener);
        plugins.util.log('Killing Express server with PID',;

// build-assets task actually processes my client js, less, images, etc and puts them to build dir
// build-server task copies server files (app.js, controllers, views) to build dir

gulp.task('serve', ['build-assets', 'build-server'], function(callback) {
        function(callback) {
            lr = tinyLr();
            lr.listen(lrPort, callback);
    ], callback);

gulp.task('watch', ['serve'], function() {
    // Reload page if built files were updated[
        buildDir + '/**/*.handlebars',
        buildDir + '/**/*.html',
        buildDir + '/**/*.js',
        buildDir + '/**/*.css'
    ], function(event) {
        var fileName = path.relative(path.join(__dirname, buildDir), event.path);
        plugins.util.log('Detected updated file ' + fileName + ', reloading server and page in browser');
            // Restart Express server

            // Send reload notification to browser
            function(callback) {
                    body: {
                        files: [fileName]

    // Perform build steps on source file modifications['app/**/*.js', '!app/**/*.spec.js'], ['build-app-js']);['app/**/*.html'], ['build-templates']);['app/**/*.less'], ['build-less']);['server/**/*.*'], ['build-server']);

You’ll need to install tiny-lr, async, gulp-util and gulp-load-plugins packages for this sample to work.
I guess that I’ll create a separate Gulp plugin for it =)

Take a look at gulp-nodemon which will restart your server when code changes.


gulp.task('develop', function () {
  nodemon({ script: 'app.js', ext: 'html js' })
    .on('restart', function () {

Live reload should work with any nodejs script. Here is a good gist.

To livereload both front and backend changes to the browser. There is also the option of not using Gulp. Then, the right combination of packages is ‘livereload’, ‘connect-livereload’, and a little help from ‘nodemon’. Here’s how they team up:

  • livereload opens a high port and notifies the browser of changed public files
  • connect-livereload monkey patches every served HTML page with a snippet that connects to this high port
  • nodemon is then used to restart the server on changed backend files

Set up livereload in Express

You’ll want to set up the Express to both start livereload server watching the public directory and ping the browser during nodemon-induced restart:

const livereload = require("livereload");
const connectLivereload = require("connect-livereload");

// open livereload high port and start to watch public directory for changes
const liveReloadServer = livereload.createServer();, 'public'));

// ping browser on Express boot, once browser has reconnected and handshaken
liveReloadServer.server.once("connection", () => {
  setTimeout(() => {
  }, 100);

const app = express();

// monkey patch every served HTML so they know of changes

Start Express with nodemon

Then you’d start the server with nodemon, for example, with a dedicated watch script npm run watch.

The key point here is to ignore the public directory that’s already being watched by livereload. You can also configure files with non-default extensions, like pug and mustache, to be watched.

"scripts": {
  "start": "node ./bin/www",
  "watch": "nodemon --ext js,pug --ignore public"

You can read a longer explanation in “Refresh front and backend changes to browser with Express, LiveReload and Nodemon.”

You can see the setup I used at

Leave a Reply

Your email address will not be published. Required fields are marked *