Gulp to watch when node app.listen() is invoked or to port (livereload, nodejs and gulp)

╄→гoц情女王★ 提交于 2019-12-05 17:39:44

Use readable event to monitor stdout of child process.

example:

nodemon({script: 'app.js',
         nodeArgs: ['--harmony'],
         stdout: false})
    .on('readable', function(data) {
        this.stdout.on('data', function(chunk) {
            if (/koa server listening/.test(chunk)) {
                console.log('livereload');
                livereload.reload();
            }
            process.stdout.write(chunk);
        });
        this.stderr.pipe(process.stderr);
    });

app.js

app.listen(3000, function(err) {
    console.log('koa server listening');
});

Here is a an example of simple and tested livereload solution based on connect server and connect-livereload and gulp-livereload plugins if that is of any help:


var gulp = require('gulp');
var connect = require('connect');
var connectLivereload = require('connect-livereload');
var opn = require('opn');
var gulpLivereload = require('gulp-livereload');

var config = {
    rootDir: __dirname,
    servingPort: 8080,

    // the files you want to watch for changes for live reload
    filesToWatch: ['*.{html,css,js}', '!Gulpfile.js']
}

// The default task - called when you run `gulp` from CLI
gulp.task('default', ['watch', 'serve']);

gulp.task('watch', ['connect'], function () {
  gulpLivereload.listen();
  gulp.watch(config.filesToWatch, function(file) {
    gulp.src(file.path)
      .pipe(gulpLivereload());
  });
});

gulp.task('serve', ['connect'], function () {
  return opn('http://localhost:' + config.servingPort);
});

gulp.task('connect', function(){
  return connect()
    .use(connectLivereload())
    .use(connect.static(config.rootDir))
    .listen(config.servingPort);
});

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!