Hi @remy!
Would you be so kind to add the listening event to nodemon states?
You can get it directly from Node: https://nodejs.org/api/net.html#net_event_listening
This is needed for example when using gulp-nodemon, since starting the Node server can be part of multiple tasks and you don't want to run something like browsersync until the server is actually listening.
I tried using the start event, but when the browser loads the server is not listening yet, so you get a blank page and need to manually reload, which is annoying.
Thanks in advance for adding this!
Cheers
Urm...but nodemon doesn't listen. It doesn't even use the net module.
Mmm, I see. Is there a way then for the app code to interact with Nodemon and decide when to trigger some ready event that could be integrated in a Gulp task?
Share your gulp file or integration of gulp-nodemon
Hi,
This is the way I wish it could work:
// Start server
gulp.task('server', function (cb) {
$.nodemon({
script: serverPath,
watch: serverPath
})
.on('ready', cb);
});
But for now I'm stuck with a less elegant solution:
// Start server
gulp.task('server', function (cb) {
$.nodemon({
script: serverPath,
watch: serverPath
})
.on('start', function () {
setTimeout(cb, 500);
});
});
@pensierinmusica you can parse stdout and trigger event (call callback) on specific message:
gulp.task('server', function (cb) {
const readyMessage = 'server started';
nodemon({
script: serverPath,
watch: serverPath,
stdout: false
})
.on('stdout', function (stdout) {
console.log('server stdout:', stdout.toString());
const isReady = stdout.toString().includes(readyMessage);
if (!isReady) { return; }
cb();
});
});
@remy close? :)
@pensierinmusica for my case I found it useful to simply start browsersync from the server itself. So, child_process.exec('gulp <post-start-task-chain-head>') at the point when you know your server is ready (say, listen).
I am having a similar issue. I reported in Browsersync repo.
Can anyone help me on this?
Thanks @vlkosinov and @sandro-pasquali for your suggestions! I ended up adopting a modified version of what @vlkosinov suggests. @sandro-pasquali method also looks nice, but decided to keep the logic of Gulp inside gulpfile.js, and not split with the main server script.
So this is the solution I adopted:
gulp.task('server', function (cb) {
$.nodemon({
script: serverPath,
watch: serverPath,
stdout: false
})
.on('stdout', function (mess) {
console.log(mess.toString());
cb();
})
.on('stderr', function (err) {
console.err(err.toString());
});
});
I guess we can close this issue now, thanks everyone!
Most helpful comment
@pensierinmusica you can parse stdout and trigger event (call callback) on specific message: