0

Following the usage part of the gulp-express specifications I'm trying to setup an express development server with livereload. I have separate tasks that do javascript uglification, transform scss to css and minify html, so I only need to run the server from my dist folder.

So far everything works, except livereload (the browser does not reload upon css changes).

My gulp task:

gulp.task('server', function() {
    server.run(['main.js'],[],[1337]);
    gulp.watch(['/dist/**/*.html'], server.notify);
    gulp.watch(['/dist/**/*.css'], server.notify);
    gulp.watch(['/dist/**/*.js'], server.notify);
    gulp.watch(['/dist/assets/images/*.*'], server.notify);
});

main.js:

// packages
var express = require('express');
var app = express();
var bodyParser = require('body-parser');
var morgan = require('morgan');
var path = require('path');

//app configuration
    // body parser
    app.use(bodyParser.urlencoded({ extended: true }));
    app.use(bodyParser.json());
    //CORS requests
    app.use(function(req, res, next) {
        res.setHeader('Access-Control-Allow-Origin', '*');
        res.setHeader('Access-Control-Allow-Methods', 'GET, POST');
        res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With, content-type, Authorization');
        next();
    });
    //log all requests to console
    app.use(morgan('dev'));

app.use(express.static(__dirname + '/dist'));
//catchall route
app.get('*', function(req, res) {
    res.sendFile(path.join(__dirname + '/dist/index.html'));
});

//start the server
app.listen(1337);
console.log('Running on port 1337!');

When running gulp server I get the following message in the console:

livereload[tiny-lr] listening on 35729 ... Running on port 1337!

I can load the page in the browser, but it won't auto-refresh on code change.

What am I doing wrong?

Miha Šušteršič
  • 9,742
  • 25
  • 92
  • 163

1 Answers1

0

you're on the road to solve your problem, I know this post is 9 months old but if it will be useful to anyone I will post a solution, it's working for me and I had the same issue as you, this was my solution:

gulp.task('webserver', function() {
    var server = gls(['app.js','public', 3000], undefined, {});
    server.start();

  //Watch files
  gulp.watch([paths.images], function (file) {
      gulp.start('custom-images');
      server.notify.apply(server, [file]);
  });
  gulp.watch([paths.scripts], function (file) {
     gulp.start('custom-js');
     server.notify.apply(server, [file]);
  });
  gulp.watch([paths.custom_styles], function (file) {
    gulp.start('custom-styles');
    server.notify.apply(server, [file]);
  });
  gulp.watch([paths.templates], function (file) {
      gulp.start('custom-templates');
      server.notify.apply(server, [file]);
  });
  gulp.watch([paths.index], function (file) {
      gulp.start('usemin');
      server.notify.apply(server, [file]);
  });
  gulp.watch([paths.build_index], function (file) {
      gulp.start('convert-index');
      server.notify.apply(server, [file]);
  });
});

As you can see, the issue is, to my understanding, that you need to pass a callback function to gulp.watch() and then call server.notify.apply(server,[file]), notice you're passing the server and the file you've modified, I suppose this is because you need to tell liveReload that this is the file you've changed. Hope this help, cheers. sigfried

I've just found this and I think it's a great solution too: https://stackoverflow.com/a/27488331/4613549

Community
  • 1
  • 1