2012-07-08 78 views
2

我试图从node.js启动couchdb,如果它尚未启动。类似于下面的代码工作像pwd但不是基本的命令为couchdb通过exec或spawn从node.js启动couchdb

var sys = require('util') 
var exec = require('child_process').exec; 
var child; 

// executes `pwd` 
child = exec("pwd", function (error, stdout, stderr) { 
    sys.print('stdout: ' + stdout); 
    sys.print('stderr: ' + stderr); 
    if (error !== null) { 
    console.log('exec error: ' + error); 
    } 
}); 

我一直在使用“couchdb” &“/usr/local/bin/couchdb”作为参数传递给高管受审。

+1

共享资格对任何人都没有价值 - 我不知道笑脸是什么。 – 2012-07-08 22:36:53

+0

+1 @Cirrostratus,-1 dscape。 – 2012-07-09 01:10:41

回答

2

我已经工作的例子,现在使用的CoffeeScript:

childproc = require "child_process"  
couchdb = childproc.spawn "couchdb" 
couchdb.stdout.setEncoding "utf8" 
buffer = "" 

couchdb.stdout.on "data", (data) -> 
    lines = (buffer + data).split(/\r?\n/) 
    buffer = lines.pop() 
    lines.forEach (line, index) -> 
    console.log line 

couchdb.stdout.on "end", -> 
    if buffer.length > 0 
     console.log buffer 
     buffer = "" 
    console.log 'process ended' 

See my gist for a fuller example in CS, Iced CS & JS

编辑 这里是JavaScript中的输出中:你认为这是一个坏主意不

var buffer, childproc, couchdb; 

childproc = require("child_process"); 

couchdb = childproc.spawn("couchdb"); 

couchdb.stdout.setEncoding("utf8"); 

buffer = ""; 

couchdb.stdout.on("data", function(data) { 
    var lines; 
    lines = (buffer + data).split(/\r?\n/); 
    buffer = lines.pop(); 
    return lines.forEach(function(line, index) { 
    return console.log(line); 
    }); 
}); 

couchdb.stdout.on("end", function() { 
    if (buffer.length > 0) { 
    console.log(buffer); 
    buffer = ""; 
    } 
return console.log('process ended'); 
}); 
+0

任何你可以发布JS输出的机会 - 相当于? – 2012-07-09 01:12:13

+1

@ClintNash我很高兴,但已经注意到Marcus已经足够为我们做这件事了。我可以为你提供一个更完整的例子,在这里我测试沙发是否在运行,如果有必要的话打开它,然后测试一个数据库是否存在,如果不存在则创建。 https://gist.github.com/733e4de4b328f4fd88dc – 2012-07-09 10:19:57

+0

+1是的,请。也许只是jsFiddle它?像这样的参赛作品可能对node.js上的交叉训练师以及那些couchdb noobs〜like-me很有价值。 #增值。 Yourock @Cirrostratus,谢谢。 – 2012-07-10 01:30:32