我正在尝试从node.js启动couchdb,如果尚未启动的话。类似下面的代码适用于pwd
等基本命令,但不适用于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
'作为exec的参数。
答案 0 :(得分: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');
});