I am trying to get gulp-livereload
to work with my nodejs server. I am using gulp-nodemon
to restart the server after changes to the files, this works. I am having trouble invoking livereload.reload()
at the correct time.
I am currently invoking livereload.reload()
on the .on('start'...
in my gulpfile.js
(whenever my nodemon starts a script). This works but it takes a few seconds. The reason is when nodemon starts to run the nodejs script it invokes livereload.listen()
before the script has invoked app.listen(port)
, so my browser refreshs without the server being ready.
Is there a way to listen if the nodejs nodemon script has invoked app.listen(port)
or perhaps listen to see if a specific port is being used?
I have fixed the issue with a small sleep but it just feels so wrong and dirty.
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');
});