2015-07-19 291 views
2

为什么下面的代码在运行全部3行之后不会导致节点退出进程?Node.js脚本不退出

var monq = require('monq') 
var client = monq('mongodb://remote.db.com:27017/mydB') 
var queue = client.queue('users') 

如果未执行第3行,脚本会退出。

+2

看起来您已将作业添加到队列中,并且继续等待被告知执行作业。 – michaelAdam

回答

-1

看起来到Mongo的底层连接在client.queue('users')被调用之前并没有建立。 monq有一个client.close()方法关闭它的数据库连接。有几个MOD可以测试这个。

var monq = require('monq') 
    var client = monq('mongodb://localhost:27017/mydB') 
    var queue; 
    setTimeout(function() { 
     // create the queue which establishes the connections 
     queue = client.queue('users'); 
     setTimeout(function() { 
      // close connection 
      client.close(); 
     }, 5000) 
    }, 5000);