在节点app.listen()被调用或端口(livereload,nodejs和gulp) [英] Gulp to watch when node app.listen() is invoked or to port (livereload, nodejs and gulp)

查看:1088
本文介绍了在节点app.listen()被调用或端口(livereload,nodejs和gulp)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 gulp-livereload 来处理我的nodejs服务器。我使用 gulp-nodemon 在对文件进行更改后重新启动服务器,这起作用。我无法在正确的时间调用 livereload.reload()



我目前正在调用<$ c在我的 gulpfile.js中的 .on('start'... > $ c> livereload.reload() (只要我的nodemon启动一个脚本)。这个工作可能需要几秒钟。原因是当nodemon开始运行nodejs脚本时,它会调用 livereload.listen() / code> 之前脚本已经调用了 app.listen(port),所以我的浏览器刷新时没有准备好服务器。 p>

有没有办法监听nodejs nodemon脚本是否调用了 app.listen(port)或者可能听到如果一个特定的端口正在被使用?



我已经解决了一个小睡眠的问题,但它只是感觉错误和脏。


<例如:

例子:

  nodemon({script:'app.js',
nodeArgs:['--harmony'],
stdout:false})
.on('readable',function(data){
this.stdout.on('data',函数(chunk){
if(/ koa服务器监听/ .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');
});


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

这篇关于在节点app.listen()被调用或端口(livereload,nodejs和gulp)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆