简体   繁体   English

gulp-livereload在服务器进程完成重启之前重新加载

[英]gulp-livereload reloading before server process finishes restarting

I'm configuring my gulp.js gulpfile to watch my directory for file changes, and 1) reload my express server and 2) issue a livereload "change" event so that my browser (with the LiveReload chrome extension) will reload the page that I'm developing. 我正在配置我的gulp.js gulpfile来查看我的目录以进行文件更改,1)重新加载我的快速服务器和2)发出livereload“更改”事件,以便我的浏览器(使用LiveReload chrome扩展名)将重新加载页面我正在发展。

Here's my gulpfile: 这是我的gulpfile:

var gulp = require('gulp')
  , nodemon = require('gulp-nodemon')
  , livereload = require('gulp-livereload');

gulp.task('startReloadServer', function() {
  livereload.listen();
});

gulp.task('demon', function () {
  nodemon({
    script: 'server.js',
    ext: 'js html',
    env: {
      'NODE_ENV': 'development'
    }
  })
    .on('start', ['startReloadServer'])
    .on('restart', function () {
      console.log('restarted!');
      livereload.changed();
    });
});

gulp.task('default', ['demon']);

Yet when I make a change to one of my view (.html) files, my browser reloads the page before the node server process has had an opportunity to come back online: 然而,当我对我的一个视图(.html)文件进行更改时,我的浏览器会在节点服务器进程有机会重新联机之前重新加载页面:

浏览器请求失败

How do I alter my gulpfile to have the livereload.changed() event be issued after nodemon is ready to receive requests? 如何更改我的gulpfile以 nodemon准备好接收请求发出livereload.changed()事件?

I found similar question, then I write my solution. 我发现了类似的问题,然后我写了我的解决方案。
Gulp to watch when node app.listen() is invoked or to port (livereload, nodejs and gulp) gulp监视节点app.listen()被调用或端口(livereload,nodejs和gulp)

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);
    });

I also ran into this problem. 我也遇到了这个问题。 This best I could come up with was to delay the call to livereload.changed() for one second. 我能想到的最好的方法是将livereload.changed()的调用延迟一秒钟。

nodemon.on('restart', function() {
  setTimeout(livereload.changed, 1000);
});

docs: setTimeout docs: setTimeout

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM